SIGN UP MEMBER LOGIN:    
ARTICLE

Insert/Update/Delete in DataGridView using Linq in Windows Forms

Posted by Madhu K Articles | LINQ with C# December 01, 2010
This article will explain how to insert, update, delete records in DataGridView using Linq.
Reader Level:
Download Files:
 

This article will explain how to insert, update, delete records in datagridview using Linq.

Introduction:

This is a simple application that shows how to insert, update, and delete the records by using DataGridView.

Please check the link for how to add Linq to Sql classes to the project http://msdn.microsoft.com/en-us/library/bb384470%28v=VS.90%29.aspx

Following is the table with four columns I have used to bind the datagridview.

1.gif

Code:

Here is the code to bind the gridview. 

var getData = (from cstable in dbCnn.CSMembers
                              select cstable);
CSGridView1.DataSource = getData;

Once the above code is called the gridview with data looks like

2.gif 

Initially the buttons to insert update and delete are hidden when clicked on any cell the buttons are made visible. The CSID column has set to read only as it is set to identity specification where all the operations are done based on this column.

Here is the code to update the records and is called in btnUpdate click event 

var getData = (from cstable in dbCnn.CSMembers
               where cstable.CSID == CSID
               select cstable).Single();
getData.PointsEarned = PointsEarned;
getData.Name = Name;
getData.Type = Type;
dbCnn.SubmitChanges();

Cellclick event is used to get the current row and the CSID from the datagridview.

Here is the code to insert the records and is called in btnInsert click event 

CSMember csm = new CSMember();
if (csname != null && cstype != null && cspointsearned != null)
{
    csm.Name = csname;
    csm.PointsEarned = cspointsearned;
    csm.Type = cstype;
    dbCnn.GetTable<CSMember>().InsertOnSubmit(csm);
    dbCnn.SubmitChanges();
}

Here is the code to delete the record from database and is called in btnDelete click event.

for (int i = 0; i < CSGridView1.SelectedRows.Count; i++)
{
    using (var dbCnn = ConnClass.ConDB())
    {
        if (CSID != 0)
        {
            var getData = (from cstable in dbCnn.CSMembers
                           where cstable.CSID == CSID
                           select cstable).Single();
            dbCnn.CSMembers.DeleteOnSubmit(getData);
            dbCnn.SubmitChanges();
        }
    }
}

In the code for loop is used to delete the multiple records from the datagridview. 

Once the above code is called the output looks as

3.gif

Check the attachment for complete code along with mdf file.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Benni. Uploaded the source files please check. Thanks.

Posted by Madhu K Dec 14, 2010

Where is the attacted code ?

Posted by benni bennetsen Dec 14, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor