Tuesday, 6 December 2011

How to populate DataGridView, GridView with SQL statement in C#

When we do the development, we always want to make the code simple and error free if possible. In C#, GridView for web based application and DataGridView for windows form based application are different in using and behavior. It looks like Microsoft has two different teams to develop GridView and DataGridView separately.  This is why I wrote this article to share the coding for each control. Here I am using MS Visual Studio 2005.

I. Populate GridView control for web based application with SQL statement

Let us put GridView1 control on the web form from Toolbox. The coding is straight forward and is like the following:

protected void Page_Load(object sender, EventArgs e)
{

string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("select * from table1", sqlConnection);
sqlConnection.Open();

SqlDataReader reader = sqlCommand.ExecuteReader();
       
GridView1.DataSource = reader;
GridView1.DataBind();
}

You run the code and you can see the result. But when you see the data binding for DataGridView in the following section, it is quite different.

II. Populate DataGridView control with SQL statement for Window form based application

When I used the DataGridView control in C# in MS Visual Studio 2005, I found DataGridView control is not friendly to use. Windows form-based DataGridView control is different from web based GridView control. DataGridView doesn't have DataBind() method.  It took me a few days to figure out.
The logic is like this
  1. Create data set from SQL statement or stored procedure
  2. Create a table to hold this data set
  3. Create a BindingSource and bind this table with this BindingSource
  4. Then bind this BindingSource with GridView control.

This looks trivial. But I found it is very efficient and error free.

Let us put DataGridView control and BindingSource control on the windows form from Toolbox. Let us name DataGridView control as dbGridView, BindingSource control as dbBindSource. Let us apply the following code:

private void Form1_Load(object sender, EventArgs e)
{
string strCon = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
string strSQL = "select * from table1";

SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dbBindSource.DataSource = table;

// Resize the DataGridView columns to fit the newly loaded content.
dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
// you can make it grid readonly.
dbGridView.ReadOnly = true; 
// finally bind the data to the grid
dbGridView.DataSource = dbBindSource;
}

Now we compile it and run it. You can see the data in the grid.

1 comment: