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

Update primary key in the Data Manager datasource

Greetings.

I have a grid, that is manually updated, without using the default syncfusion CRUD methods. I use negative id's to mark "new" records in the grid. Hovewer, when the record is saved in the database, a unique GUID is attached to the record object. It is saved in database, but i need to update it in the grid (so i wont have to reload all the records from the server). Therefore, I want to change the Id of the record. I have the old record, I have the new record that came from the server, but the datamanager "update" method seems to require the primary key — the unique field, that i, besides the id, dont have. Is it possible to still somehow change the record?

3 Replies

TS Thavasianand Sankaranarayanan Syncfusion Team March 7, 2017 04:03 PM UTC

Hi Rykunov, 

Thanks for contacting Syncfusion support. 

Query: The unique field, that I, besides the id, dont have. Is it possible to still somehow change the record? 
 
We have analyzed your query. We suspect that you want to generate an unique code for the grid record, which used to update the grid record in client side itself. 

If you have defined the IsIdentity column as Auto Generated in the DB. While inserting the new records to the DB, the system will auto-generate the value for the PrimaryKey column. So, while adding new record we need not enter value for the particular column it will be autogenerate the value and field will be in read-only. 

Refer the below code example. 


$("#Grid").ejGrid({ 
                // the datasource "window.gridData" is referred from jsondata.min.js 
               dataSource: window.gridData, 
               allowPaging: true, 
              
               ------------                 
 
                columns: [ 
                        { field: "OrderID", isIdentity: true, isPrimaryKey: true, headerText: "Order ID", textAlign: ej.TextAlign.Right, width: 90 }, 
                        { field: "CustomerID", headerText: "Customer ID", width: 90 }, 
                        { field: "ShipCity", headerText: "Ship City", width: 90 }, 
                         
                        ----------------- 
 
               ] 
                            
           }); 



Refer the help documentation. 



Regards, 
Thavasianand S. 



RA Rykunov Alex March 10, 2017 07:29 AM UTC

Thank you, hovewer, that does not solve my problem. I want to generate the guid myself — on the server. 

For now I delete the old record and then insert a new one, but that is not a preferable way to solve the problem.


TS Thavasianand Sankaranarayanan Syncfusion Team March 14, 2017 04:25 PM UTC

Hi Rykunov, 
 
We have analyzed your query. We have prepared a sample with the auto generated value in the server side for the column isIdentity column.  
 
Refer the below code example. 
 
[app.component.html] 
 
<ej-grid #grid  [dataSource]="gridData" [allowScrolling]=true > 
 
 
    <e-columns> 
             <e-column field="OrderID" [isPrimaryKey]="true" [isIdentity]="true" width="75" textAlign="right"></e-column> 
        <e-column field="EmployeeID" headerText="Employee ID" [validationRules] = "{ required: true, number: true }" width="75" textAlign="right"></e-column> 
        <e-column field="CustomerID" headerText="CustomerID" width="80"textAlign="right"></e-column> 
        
    </e-columns> 
 
</ej-grid> 
 
 
 
If you are using CRUD operations, we suggest to return the update records to client end. Since you are using the IsIdentity for the PrimaryKey column, the value in the client-side differs from the server-side and to notify the changes on the IsIdentity column you have to return them from server-end. Refer to the following code example. 

[HomeController.cs] 
         
public ActionResult Update(Orders value) 
       { 
 
            var data = order.ToList(); 
            Orders result = data.Where(o => o.OrderID == value.OrderID).FirstOrDefault(); 
            if (result != null) 
            { 
                result.OrderID = value.OrderID; 
                result.CustomerID = value.CustomerID; 
                result.EmployeeID = value.EmployeeID; 
                 
            } 
           return Json(value, JsonRequestBehavior.AllowGet); 
        } 
        public ActionResult Insert(Orders value) 
        { 
            int generate = order.ToList().Count(); 
 
            value.OrderID = ++generate; // set the auto generated value to the OrderID column 
 
            order.Insert(0, value); 
       
            return Json(value, JsonRequestBehavior.AllowGet); 
        } 
        public ActionResult Delete(int key) 
        { 
            var data = order.ToList(); 
            Orders result = data.Where(o => o.OrderID == key).FirstOrDefault(); 
            order.Remove(result); 
            return Json(data, JsonRequestBehavior.AllowGet); 
        } 
 
 
 
 
We have prepared a sample and it can be downloadable from the below location. 
 

We have included angular2 sample also with in the project. 

Note:  First run angular2 sample separately for including packages and then run the project and change the path as (http://localhost:49349/angular2/ejGrid_Vpaging%20-%20Copy/ejGrid_Vpaging/) to run the sample in angular 2 
 
Regards, 
Thavasianand S. 


Loader.
Up arrow icon