August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with the DataGridView Control

  • December 28, 2007
  • By Bipin Joshi
  • Send Email »
  • More Articles »

Introduction

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,
                                    "CustomerID");
      pInsert[1] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
                                    "CompanyName");
      pInsert[2] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
                                    "ContactName");
      pInsert[3] = new SqlParameter("@p4", SqlDbType.VarChar, 40,
                                    "Country");

      pUpdate[0] = new SqlParameter("@p2", SqlDbType.VarChar, 40,
                                    "CompanyName");
      pUpdate[1] = new SqlParameter("@p3", SqlDbType.VarChar, 40,
                                    "ContactName");
      pUpdate[2] = new SqlParameter("@p4", SqlDbType.VarChar, 40,
                                    "Country");
      pUpdate[3] = new SqlParameter("@p1", SqlDbType.VarChar,  5,
                                    "CustomerID");

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

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

      cmdInsert.Parameters.AddRange(pInsert);
      cmdUpdate.Parameters.AddRange(pUpdate);
      cmdDelete.Parameters.AddRange(pDelete);

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

   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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel