We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Primary Key Issue

Hi Team

I use ej2 angular gird for CRUD operations. The data exchange with the server is done using angular service through HttpClient. I set the data source similar to setting a local JSON data.

  • If I do not set the Id col as primary key , 
  • Adding and updating a blank grid works perfectly
  • But when  the saved data returned from the server cannot be correctly edited and saved. It will just update the first row.
  • If I set the Id coulumn as Primary key, 
  • adding and updating to blank grid before communicating with server is not possible.
  • Data returned from the server works fine
Please advice a solution


3 Replies

TS Thavasianand Sankaranarayanan Syncfusion Team February 12, 2019 06:41 AM UTC

Hi Sahal, 

Greetings from Syncfusion. 

Based on your query we suspect that you want to do CRUD operations in Grid. By default, if you are using CRUD operation in Grid then we need to have isPrimaryKey column, which has the unique value. So, we suggest you to set the isPrimaryKey for particular column in Grid. Also if we have blank values in Grid then while editing that row it will shown the proper values in edit form because it is not fetching the proper value when isPrimaryKey column value as null. 



Please let us know if you need further assistance on this. 
  
Regards, 
Thavasianand S. 



SA Sahal February 12, 2019 07:56 AM UTC

Usually data stored in the database have an identity column and a primary key. ie on insert the primary key is incremented. If we set that field as the primary key for the grid whenever a new row is added, it will have a null value and hence the editing won't work properly.

If we somehow increment this field and set it on the grid, when we sent the data to the db, the newly incremented values may or may not be present and will lead to an inconsistent edit/insert operation

Could you please provide sample for my scenario

In my opinion the desired behavior of the grid should be that when a primary key is set by the code, the grid should automatically create a hidden primary key and manage the CRUD operation. This is how Microsoft manages in their DataGridView Control.


MS Madhu Sudhanan P Syncfusion Team February 14, 2019 09:31 AM UTC

Hi Sahal, 

Thanks for your update. 

Query : the desired behavior of the grid should be that when a primary key is set by the code, the grid should automatically create a hidden primary key and manage the CRUD operation 

We have analyzed your query and by default, the grid must be configured with a primary key column to perform the CRUD operations.  
 
In your scenario, you have provided the data source as local array and the save operation is performed externally to the server hence the primary key obtained after the ‘Update’ action is not reflected in the grid. To resolve this, we suggest to get the updated data from the database and provide it to the grid once again in the actionComplete event when args.requestType is ‘save’.  
 
Also please provide the below information for us to analyze your query and provide a better solution as soon as possible, 

  1. Share the full grid code example?
  2. Share the reason for setting the grid data source with local data?
  3. Share the server side code example?
  4. Share your exact grid requirement for us to create a custom sample?

Please get back to us for further assistance. 

Regards, 
Madhu Sudhanan P 


Loader.
Up arrow icon