January 27, 2021
Hot Topics:

Working with the DataGridView Control

  • By Bipin Joshi
  • Send Email »
  • More Articles »


Displaying data in tabular format is one of the common requirements in Windows-based applications. No wonder the DataGridView control is popular amongst Windows Forms developers. The DataGridView control has many enhancements over the earlier DataGrid and it is worthwhile for any Windows developer to master this control. In this article, I am going to illustrate some of the features that were missing (or tedious to implement) in the earlier versions of .NET. Specifically, I will show:

  • How to perform data binding with DataSet
  • How to add button and combo box columns to the grid
  • How to validate data in the grid

Sample Scenario

Throughout the examples, you will use the Customers table of the Northwind database. So, make sure that you have installed the Northwind database in your installation of SQL Server. Although the Customers table contains many columns, you will restrict your examples to use the CustomerID, CompanyName, ContactName, and Country columns only. You will develop a Windows application that displays data from Customers table using the DataGridView control.

To begin with, create a new Windows application using C# as the programming language (see Figure 1).

Click here for a larger image.

Then, add a new class named Customer to the project and add the following code to it.

public class Customer
   private static string strConn;

   static Customer()
      strConn = "data source=.;initial catalog=northwind;
      user id=sa;password=sa";

   public static DataSet GetDataSet()
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter("select customerid,
         companyname,contactname,country from customers", strConn);
      da.Fill(ds, "customers");
      return ds;

   public static void UpdateDataSet(DataSet ds)
      SqlConnection cnn = new SqlConnection(strConn);

      string sqlInsert, sqlUpdate, sqlDelete;
      sqlInsert = "insert into customers(customerid,companyname,
         contactname,country) values(@p1,@p2,@p3,@p4)";
      sqlUpdate = "update customers set companyname=@p2,
         contactname=@p3,country=@p4 where customerid=@p1";
      sqlDelete = "delete from customers where customerid=@p1";

      SqlParameter[] pInsert = new SqlParameter[4];
      SqlParameter[] pUpdate = new SqlParameter[4];
      SqlParameter[] pDelete = new SqlParameter[1];

      pInsert[0] = new SqlParameter("@p1", SqlDbType.VarChar,  5,
      pInsert[1] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
      pInsert[2] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
      pInsert[3] = new SqlParameter("@p4", SqlDbType.VarChar, 40,

      pUpdate[0] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
      pUpdate[1] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
      pUpdate[2] = new SqlParameter("@p4", SqlDbType.VarChar, 40,
      pUpdate[3] = new SqlParameter("@p1", SqlDbType.VarChar,  5,

      pDelete[0] = new SqlParameter("@p1", SqlDbType.VarChar,  5,

      SqlCommand cmdInsert = new SqlCommand(sqlInsert,cnn);
      SqlCommand cmdUpdate = new SqlCommand(sqlUpdate,cnn);
      SqlCommand cmdDelete = new SqlCommand(sqlDelete,cnn);


      SqlDataAdapter da = new SqlDataAdapter();
      da.InsertCommand  = cmdInsert;
      da.UpdateCommand  = cmdUpdate;
      da.DeleteCommand  = cmdDelete;
      da.Update(ds, "customers");

   public static DataSet GetCountries()
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter("select distinct
         country from customers", strConn);
      da.Fill(ds, "countries");
      return ds;

Page 1 of 5

This article was originally published on December 28, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date