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

How to save New Row in Entity Framework

I am using Entity Framework code first in my application. I have a AddNewRow and having trouble to first find the right event that would capture addnewrow. I have used CollectionChanged event
(sfDataGridProposals.View as CollectionViewAdv).CollectionChanged += sfDataGridProposals_CollectionChanged;

private void sfDataGridProposals_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
               
                Proposal newRecord = e.NewItems as Proposal;
                Proposal proposal = new Proposal();
                proposal.ProposalActive = newRecord.ProposalActive;
                proposal.ProposalAwardDate = newRecord.ProposalAwardDate;
                proposal.ProposalCompanyID = newRecord.ProposalCompanyID;
                proposal.ProposalDueDate = newRecord.ProposalDueDate;
                proposal.ProposalDueTime = newRecord.ProposalDueTime;
                proposal.ProposalEngineerID = newRecord.ProposalEngineerID;
                proposal.ProposalGroupID = newRecord.ProposalGroupID;
                proposal.ProposalName = newRecord.ProposalName;
                proposal.ProposalNo = newRecord.ProposalNo;
                proposal.ProposalNotes = newRecord.ProposalNotes;
                proposal.ProposalOwnerID = newRecord.ProposalOwnerID;
                proposal.ProposalStatusID = newRecord.ProposalStatusID;
                proposal.ProposalSubmitted = newRecord.ProposalSubmitted;
                proposal.ProposalSubmittedDate = newRecord.ProposalSubmittedDate;

                _MainFormContext.Proposals.Add(proposal);

                _MainFormContext.SaveChanges();

            }
        }
However it gives me an error newRecord was null.
Is there any other way to addnewrow?

1 Reply

SA Saravanan Ayyanar Syncfusion Team January 20, 2020 02:49 PM UTC

Hi Mariusz Juszkiewicz, 
 
Thank you for using Syncfusion controls. 
 
In your code snippet, you are tried to get the e.NewItems directly. But it is collection type, so you need to get the values by index. In this collection row values are in RecordEntry type, So you need to change as RecordEntry type. Please refer the below code snippet.   
 
(sfDataGrid1.View as CollectionViewAdv).CollectionChanged += Form1_CollectionChanged; 
 
private void Form1_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
 { 
    if(e.Action==System.Collections.Specialized.NotifyCollectionChangedAction.Add) 
     { 
         OrderInfo newRecord = ((e.NewItems[0] as RecordEntry).Data as OrderInfo); 
         OrderInfo orderInfo = new OrderInfo(); 
         orderInfo.OrderID = newRecord.OrderID; 
         orderInfo.ContactNumber = newRecord.ContactNumber; 
         orderInfo.CustomerID = newRecord.CustomerID; 
         orderInfo.OrderDate = newRecord.OrderDate; 
         orderInfo.ProductName = newRecord.ProductName; 
         orderInfo.Quantity = newRecord.Quantity; 
         orderInfo.ShipCountry = newRecord.ShipCountry; 
         orderInfo.UnitPrice = newRecord.UnitPrice; 
     } 
 }      
 
Sample link: 
 
Please let us know, if you require further assistance on this. 
 
Regards, 
Saravanan A. 


Loader.
Live Chat Icon For mobile
Up arrow icon