SIGN UP MEMBER LOGIN:    
ARTICLE

DataGridView - Insert, Update, Delete and Retrieve records using stored procedure

Posted by Suthish Nair Articles | ASP.NET Controls in C# October 18, 2010
This article is about DataGridView control that shows some basics data operations like inserts, updates, deletes and retrieve records using stored procedure.
Reader Level:
Download Files:
 

Article is about DataGridView control that shows some basics data operations like inserts, updates, and deletes using stored procedure.

Introduction

DataGridView control is the most popular amongst Windows Forms developers. Control helps to display data in a tabular format. The DataGridView control allows viewing, inserting, updating, or deleting data in a db table. Here am trying to show a simple example, how to retrieve, add/update or delete records using DataGridView. 

Getting Started

Before getting started this sample uses Northwind database and Visual Studio 2008. 


Following namespaces used:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

This sample does not using any table from Northwind, but created a simple table named (dbo.UserMaster). It's a simple User Master Table using for saving First Name, Last Name, City and Phone Number. Also, contains one identity column (SNo). Stored Procedure using [dbo].[UserDetailsSummary].

1.gif 
Now create a windows application project, drag and drop DataGridView control to Form window. Set true for "Enable Adding", "Enable Deleting" and "Enable Editing" from DataGridView designer. This will enable control for editing, adding, deleting records. Add a checkbox control into DataGridView which uses as a selection for deletion of records.

2.gif

The Code

Form contains three buttons: 

Retrieve, Add/Update (Enabled False) and Delete (Enabled False).

btnRetrieve_Click: Retrieving records from db table which uses stored procedure by passing input parameters (method used: AddParameteres) which check for records exists. If found returning all the rows from User Master Table or if not returning a dummy row. If more than one row found, buttons Add/Update and Delete property changing to Enabled True.

dgvUsers_DataBindingComplete: Once DataGridView data binding completes, this events call to set column (SNo) property readonly to true. This will helps to disable editing of identity column.

btnAddEdit_Click: Event used for adding or editing records by looping through the DataGridView control. SqlTransaction used for successful commit or rollback if any error occurs.

btnDelete_Click: Event used for deleting records depending upon checkbox selection of DataGridView control.

Summary

Code samples attached also with sql scripts. Test the code and post your comments. There will be different better ways of doing these DataGridView operations. If anyone interested in making an article, please come forward and share the knowledge.

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

Mr. Raj. You can download the samples attachment from above link.

Posted by Suthish Nair Mar 30, 2011

pls full coding send me mail. DataGridView - Insert, Update, Delete and Retrieve records using stored procedure. Thankyou Mr.suthish

Posted by bhagyaraj bruce Mar 30, 2011

you need to explore it. check for articles here, might get some help.

Posted by Suthish Nair Mar 01, 2011

Hi, Is there any way to add edit/delete links or buttons to the same column in data grid view.

Posted by Kiran Vidhate Mar 01, 2011

Create a new Windows Application Project, and add attached zipped files. Run the sql scripts. Build and run the project.

Posted by Suthish Nair Feb 12, 2011
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