Home > ADO.net, Tutorial for Beginners > DataGrid In CSharp

DataGrid In CSharp

If you are new to ADO.Net please visit our Ado.net section:

This is very flexible and popular control in Asp.net. We can use this tool to display data in Web/Windows environment.  It helps developers to develop a data driven application in .Net. It allows relationship; we can create relationship on the fly. We can update, delete our data at runtime.  Datagrid has some essential features like Sorting & Pagination. Sorting enables user to sort any columns and paginations helps grid in faster loading of data. User can navigate from one page to another. You can set the size of per page(How many records you want to display in one go).

DataGrid In CSharp

Data is coming in tabular form, even I have implemented the sorting features on all columns( columns are underlined).  
We can display data from single table or we can set the relationship kind of hierarchical relationship between tables. 


How to bind datagrid with Datasource: Binding of a grid to the datasource is very easy and straightforward you can bind your datagrid in two simple steps as

  • Define the Datasource : “DataGrid1.DataSource = dataSourceName”;
  • And call databind method of DataGrid: “DataGrid1.DataBind();”

Type of Datasource:  In Asp.net all the components those implements the IListsource interface can be the Datasource few are here

  • DataSet
  • DataTable
  • DataView
  • Array

Before displaying data in datagrid, you must have defined the connection and command objects.  I am implementing connections basic details in Web.config as below

   <add key="strConnectionString"
   value="data source=MyPC\SQLEXPRESS;
   user=sa;password=;initial catalog=;Pooling=False "/>


Then you can create SqlConnection object as

SqlConnection objConn = new SqlConnection(ConfigurationManager.AppSettings["Keyname"]);

KeyName will be your ="strConnectionString". ConfigurationManager.appsettings help us to read the key from web.config file.

And now define the command object:

SqlCommand cmd = new SqlCommand("Your Query", objConn);


After that you can get the data in DataSet:

SqlConnection objCon = new SqlConnection
       SqlCommand cmd = new SqlCommand("Query", objCon);
       SqlDataAdapter DA = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();


If you are new to ADO.Net please visit our Ado.net section:

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.