SIGN UP MEMBER LOGIN:    
ARTICLE

Database Updates From DatagridView

Posted by Uma Shankar Articles | ADO.NET in C# January 06, 2012
A DataGridView control can be used to display data from a database. Here you will learn how we can update our database from a DatagridView.
Reader Level:
Download Files:
 

Introduction

A DataGridView control can be used to display data from a database. A table can be displayed from a database in a DataGridView, using the DataAdapter class and data logic. You can easily update a database from a DataGridView.

DataGridView1.gif

Step 1

Create a Windows Forms Application Project, and place a DataGridView on the form.
 
Step 2

Click on the pin button on right-up side of DataGridView and click add project DataSource. And follow these simple steps.

  • Choose DataSourceType Database
  • New Connection, Make Connection
  • Select Table and Column names
  • Finish

DataGridView2.gif

DataGridView3.gif

Step 3

Your DataSet now has been created. You can see it from the Solution Explorer.

DataGridView4.gif

Step 4

See the properties of the Adapter on DataSet, whether it has a query for updating or not. If not then add an update query.

datagrideviewdata.gif

Step 5

A line is automatically generated, when you bind the DataGridView to the dataset. As:

private void testGrid_Load(object sender, EventArgs e)
{
     
// TODO: This line of code loads data into the 'gridTestDataSet.Emp' table. You can move, or remove it, as needed.
     this.empTableAdapter.Fill(this.gridTestDataSet.Emp); 
}

On Click Event of Update button

private void button3_Click(object sender, EventArgs e)
{
     
//Update button update dataset after insertion,upadtion or deletion
     DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
     if (dr == DialogResult.Yes)
     {
          this.empTableAdapter.Update(gridTestDataSet.Emp);
          dataGridView1.Refresh();
          MessageBox.Show("Record Updated");
     }
}


Points of Interest

You can insert a row and click the update button; a row in the database will be inserted.

If you update any cell and click the update button, then the database will be updated.

To delete, select the rows and then press the Delete key and click the update button; the rows will be deleted. All your insertions, updates, and deletions will be effected by the single button.

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

it is very nice code and trick

Posted by Bhushan Inje Mar 26, 2012

Useful for me

Posted by Sonakshi Singh Feb 06, 2012

Good start, Welcome to the C# Corner.

Posted by Dinesh Beniwal Jan 06, 2012

Well done

Posted by Akash Ahlawat Jan 06, 2012

Thanks for sharing

Posted by Arjun Panwar Jan 06, 2012
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor