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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to save New Row in Entity Framework

Thread ID:

Created:

Updated:

Platform:

Replies:

150778 Jan 20,2020 01:09 AM UTC Jan 20,2020 02:49 PM UTC WinForms 1
loading
Tags: SfDataGrid
Mariusz Juszkiewicz
Asked On January 20, 2020 01:09 AM UTC

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?

Saravanan Ayyanar [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon