Click here to Skip to main content
13,829,149 members
Click here to Skip to main content
Add your own
alternative version


103 bookmarked
Posted 15 Jul 2002
Licenced CPOL


, 15 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Using the ASP.NET DataGrid for fun and profit


The ASP.NET DataGrid control is very powerful and versatile. A dataset can be bound and displayed with almost no effort and extending the control to fit the needs of a project is very easy as we will see in this tutorial.


The simplist way to use a DataGrid is to allow the .NET library to do the work. This is easily accomplished by setting the AutoGenerateColumns property to true and binding it to a datasource.

DataGrid1.DataSource = sqlCmd.ExecuteReader();

The above code will assign the SqlDataReader returned from the SqlCommand.ExecuteReader method to the DataGrid1.DataSource. The call to DataGrid1.DataBind() will cause the control to iterate through the DataReader and create the columns and populate the rows appropriately for the contents.

Bound Columns

One drawback with the AutoGenerateColumns method is that all columns in the dataset will be displayed. If you want more control you need to use bound columns. To access the DataGrid properties dialog, either right click on the DataGrid on your form and choose Property Builder, or click on the ellipse button next to the columns collection in the property browser.

To add a bound column, select it the Available columns listbox then click the arrow button. Select the column in the Selected columns listbox, First Name in this case. When a new column is added it will be displayed as Bound Column in the listbox. Now you can set the propeties necessary to display the column in you DataGrid. If you want to display a name in the header for this column enter it in the Header text textbox. This will also change the name in the Selected columns listbox. Enter the field name from the dataset you wish to bind to this column in the Data Field textbox.

The Data formatting expression textbox is used to apply any formatting to the displayed data. For instance if the field is numeric and you want it displayed as a currency you would enter {0:c} in this field. The Visible checkbox can be used to hide columns you want populated but not visible. The ordering of the columns can be changed by moving them up or down in the Selected columns listbox.

Hyperlink Columns

Hypertext Columns are used to obviously display a hypertext link in the data column. The text of the link can be configured in the Text textbox and the URL in the URL text box. As with bound columns the Text field textbox is used to set the dataset field to bind this column to. This will only set the text of the hyperlink not the URL. The URL field is used to bind the link to a dataset field. The URL format string is used to format the hyperlink if necessary.

Template Columns

To get more creative with how the DataGrid is rendered we need to use Template Columns. Any column can be changed into a Template Column by clicking the link on the DataGrid properties page. As can be seen in this code snippet the default template uses a label control for normal display and a textbox control while in edit mode.

<asp:TemplateColumn HeaderText="First Name">
        <asp:Label runat="server" 

        Text='<%# DataBinder.Eval(Container, "DataItem.au_fname") %>' />
        <asp:TextBox runat="server" 

        Text='<%# DataBinder.Eval(Container, "DataItem.au_fname") %>' />

The Text property of these controls are set by using the DataBinder.Eval method. This static method uses reflection to evaluate the first parameter at runtime. In this case, Container is a DataRow from the DataReader that is bound to the DataGrid. The second parameter, DataItem.au_fname, represents the column in the DataRow. The parameters can also be written as Container.DataItem and "au_fname", which makes it slightly more clear.

Edit Mode

To place the DataGrid in edit mode you need to set the EditItemIndex property to a valid row index and rebind the DataGrid. The DataGrid will be rendered again but this time the EditTemplate will be used for the indicated row.

DataGrid1.EditItemIndex = 1;

Although the DataGrid.EditItemIndex can be set at anytime the most user friendly way is to create a Button Column. Expanding the Button Column item in the Available columns listbox will show three predefined button columns. For our purposes add the Edit, Update, Cancel Button Column. We will leave the default values for the properties but you can change the text displayed for each by editing the appropriate textbox. The Button type combobox is used to select how the button will be displayed, either as a hyperlink or normal button.

As can be seen here the EditCommandColumn will be added to the DataGrid. During normal rendering the column will contain an Edit linkbutton, when in edit mode the column will contain both an update and cancel linkbutton.

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" 
     CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>

Because these are CommandButtons you must handle the events generated and provide any necessary processing. Events can be added from the Events view of the property browser for the DataGrid.

In the Edit event handler the Item.DataSetIndex can be used to set the DataGrid.EditItemIndex. This property contains the index within the DataGrid of the row the event was generated from.

private void OnEdit(object source, 
                    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    DataGrid1.EditItemIndex = e.Item.DataSetIndex; BindData(); 

To cancel the edit simply set the DataGrid.EditItemIndex to -1 and rebind the DataGrid. In the Update event handler you can take whatever action is necessary to update your datasource.

More Creative Editing

The above method works fine if you want to use TextBoxes for the editing but what if you want the user to be able to enter values from a selected range. In this case a bit more creativity is needed. In the EditItemTemplate, the Textbox control can be replaced with a Dropdownlist control.

<asp:dropdownlist DataSource='<%# LoadNames() %>' 

            DataTextField="au_fname" runat="server"></asp:dropdownlist>

As can be seen here the DataSource property is set at runtime by a call to the LoadNames function.

public ICollection LoadNames()
    SqlConnection sqlNewConnection = new SqlConnection();
    sqlNewConnection.ConnectionString = sqlConnection1.ConnectionString;

    SqlDataAdapter da = new SqlDataAdapter("SELECT au_fname FROM authors", 
    DataSet ds = new DataSet();
    return ds.Tables[0].DefaultView;

Note that a new SqlConnection must be created to obtain the DataSet for the DropDownList. This is because databinding is still occurring on the DatGrid and the SqlConnection object is still in use at this point. The DropDownList control is bound to an ICollection derived object, in this case it is the DefaultView from the first DataSet table.

After the user has made changes and clicked the Update link, the OnUpdate method will be called. This is where it can be determined which item was selected in the dropdownlist. As can be seen in this code using the FindControl method of the DataGridCommandEventArgs.Item property will return the DropDownList. You can then obtain the SelectedItem text or value depending on needs.

private void OnUpdate(object source, 
                      System.Web.UI.WebControls.DataGridCommandEventArgs e)
    // Find the drop down list
    DropDownList DropList = (DropDownList)e.Item.FindControl("dlNames");
    string strTest = DropList.SelectedItem.Text;

    // Process updated data here

    DataGrid1.EditItemIndex = -1;

Checkboxes and TextBoxes

In the case of a shopping cart type application where the user would enter a quantity or select a checkbox you need to process the DataGrid a little differently. In this example only the ItemTemplate will be used. Here a TextBox has been added for the user to enter a quantity. The OnTextChanged event must either be entered by hand, or you can create the TextBox control outside the DataGrid then copy it. 

<asp:TemplateColumn HeaderText="Qty">
        <asp:TextBox id="TextBox1" runat="server" 

                     Columns="3" MaxLength="3" OnTextChanged="OnQtyChanged">

The OnTextChange handler is necessary because of the order events are processed when the form is submitted. When the submit button is clicked the form is posted to the server and the DataGrid is rebound which replaces any values that had been entered into the TextBoxes. By using the OnTextChanged event you have the chance to catch the value entered and store it for later use. As can be seen here the DataGridItem is obtained from the sender parameter. The TextBox control is the second control in the first cell, the first control in the cell is a Literal Control so we skip it. In this example the Text property is converted to an int and placed in the HashTable.

Checkboxes or other controls can be used in a similar manner.

protected void OnQtyChanged( object sender, System.EventArgs e)
    // Get the DataGrid row from the sender
    DataGridItem Item = (DataGridItem)
    // Get the TextBox control in the first cell
    TextBox txtQty = (TextBox)Item.Cells[0].Controls[1];
    // The au_id field is not an int but if it were...
    //int nID = (int)DataGrid1.DataKeys[Item.ItemIndex];
    // For this demo just use the ItemIndex
    int nID = Item.ItemIndex;

    // Now add to the HashTable
    hashQty.Add(nID, int.Parse(txtQty.Text));


It would take a complete book to cover all the features of the DataGrid effectively but hopefully this will give you the basics necessary to begin using it in your projects. Other areas of interest are the Template styles which can give your DataGrid a distinctive look.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionHow to open 2 mails with the mailto command Pin
jpa-lnf27-Apr-09 0:18
memberjpa-lnf27-Apr-09 0:18 
QuestionAbout Data Grid Pin
jay198517-Aug-06 23:18
memberjay198517-Aug-06 23:18 
AnswerRe: About Data Grid Pin
Not Active18-Aug-06 4:04
mentorNot Active18-Aug-06 4:04 
GeneralDropdownlist in datagrid Pin
rahul_l1014-Jun-06 17:51
memberrahul_l1014-Jun-06 17:51 
GeneralRe: Dropdownlist in datagrid Pin
Not Active15-Jun-06 3:32
mentorNot Active15-Jun-06 3:32 
GeneralRe: Dropdownlist in datagrid Pin
rahul_l1015-Jun-06 12:09
memberrahul_l1015-Jun-06 12:09 
QuestionDatagrid with Listbox Pin
hamdanimars16-May-06 5:24
memberhamdanimars16-May-06 5:24 
QuestionHow to use EditItemTemplate in datagrid....... Pin
deeban21-Nov-05 7:47
memberdeeban21-Nov-05 7:47 
GeneralDisplay thumbnails in datagrid Pin
j1webb11-Sep-05 6:45
memberj1webb11-Sep-05 6:45 
GeneralRe: Display thumbnails in datagrid Pin
Not Active11-Sep-05 7:43
mentorNot Active11-Sep-05 7:43 
Generalintelligent datagrid list Pin
pantaloo21-Aug-05 23:47
memberpantaloo21-Aug-05 23:47 
Generalintelligent datagrid list Pin
pantaloo21-Aug-05 23:44
memberpantaloo21-Aug-05 23:44 
Generalmore &quot;controlled&quot; textbox formatting Pin
pcorke3-Mar-05 2:57
memberpcorke3-Mar-05 2:57 
GeneralRe: more &quot;controlled&quot; textbox formatting Pin
Not Active3-Mar-05 9:54
mentorNot Active3-Mar-05 9:54 
GeneralRe: more &quot;controlled&quot; textbox formatting Pin
pcorke3-Mar-05 21:57
memberpcorke3-Mar-05 21:57 
GeneralRe: more "controlled" textbox formatting Pin
Not Active3-Mar-05 22:39
mentorNot Active3-Mar-05 22:39 
GeneralRe: more &quot;controlled&quot; textbox formatting Pin
pcorke6-Mar-05 21:52
memberpcorke6-Mar-05 21:52 
GeneralTextbox Formatting Pin
msogun3-Feb-05 16:04
membermsogun3-Feb-05 16:04 
GeneralRe: Textbox Formatting Pin
Not Active3-Feb-05 16:54
mentorNot Active3-Feb-05 16:54 
Generalwhy &quot;#region&quot; Pin
david20002-Jan-05 7:37
memberdavid20002-Jan-05 7:37 
GeneralRe: why &quot;#region&quot; Pin
Not Active2-Jan-05 9:58
mentorNot Active2-Jan-05 9:58 
GeneralRe: why "#region" Pin
david20007-Jan-05 10:12
memberdavid20007-Jan-05 10:12 
QuestionIs DataGridDemo a webapplication? Pin
david200028-Dec-04 13:15
memberdavid200028-Dec-04 13:15 
AnswerRe: Is DataGridDemo a webapplication? Pin
Not Active28-Dec-04 15:31
mentorNot Active28-Dec-04 15:31 
Generalrunnig the DataGridDemo Pin
david200023-Dec-04 10:27
memberdavid200023-Dec-04 10:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 16 Jul 2002
Article Copyright 2002 by Not Active
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid