How To Use DataGrid Webcontrol On ASP.Net Page
As the title of the article suggests, it is about a very simple concept i.e.
how do you include a DataGrid control on your web page. If you are already
using a DataGrid control, then you already know all the stuff discussed here.
We are not going to discuss any thing special in this article. This is more for
a first time user. A lot of material discussed here can be found in .Net
framework documentation. But for starting this article series, we needed a
base. So we decided to start from here. A new user may find this article like a
getting started type.
What is DataGrid?
As the name suggests, it is a grid that can be used to display information in
some tabular form. Notice that we have used the term information and not data.
When we use the term data, it gives an impression that some fields and
tables will be shown from a database. This control is not just limited to
displaying data from a database table. It can be used for displaying any
How do you include this control in a page?
If you are using Visual Studio .Net, then its just a matter of simply dragging
DataGrid control from the toolbox on to the page and the IDE
will take care of adding the appropriate tags in your
And then you will be able to specify all the properties for the control in the
Properties view. But in this article we will show how you can
include this control on your page without using IDE and directly adding the
appropriate tags on your page. This approach will also give you more
understanding of the control.
To include the control on your page, you just need to add the tag for this
control on the page, specify its ID and the most important part, specify
<form id="Form1" method="post" runat="server">
<asp:DataGrid ID="DefaultGrid" Runat="server">
How is information bound to DataGrid?
To display information in the grid, it needs a data source. This datasource is
not just limited to ADO.Net components like DataReader, DataSet, etc. This
source can be any object that implements
For this article we took the sample from the documentation and customized for
our own purpose. A new
DataTable object is created a couple of
objects are added to it with 3
DataColumn objects. And thats it.
We have the data source for our grid.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Index", typeof(Int32)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dr1 = dt.NewRow();
dr1 = 1;
dr1 = "Simple";
dr1 = "A simple use of DataGrid control";
dr2 = dt.NewRow();
dr2 = 1;
dr2 = "Hyperlinks";
dr2 = "Adding hyperlink columns to DataGrid control";
DataView dv = new DataView(dt);
private void Page_Load(object sender, System.EventArgs e)
// Attach the data source to grid.
DefaultGrid.DataSource = this.CreateDataSource();
// Make the grid databoud.
Any more steps?
These are the only steps we need for a very simple use of
web control on a page.