Problem solve Get help with specific problems with your technologies, process and projects.

Creating a datagrid with specific conditions

I would appreciate if you could provide info on how to handle a basic application using ASP.NET for the below mentioned scenario.

There is a datagrid on a page with about 10 rows and about eight columns. Except for the second column, all other columns contain the read-only data, and the second column contains all the text boxes with the database table column values. Also, at the bottom of the datagrid there is a single Update button.


  1. The user can update either a single text box value in a row or he can update multiple text box values in any number of rows. After the user is done changing the values and hits the Update button, then all the changes made in the datagrid have to be reflected in the database.

  2. Only numeric values are allowed to enter into that text boxes.
  3. The numeric value entered cannot be greater than some maximum limit, say 1000.

  4. If the user enters invalid data, then the application should display an error message containing the invalid data entered by the user at the top of the grid. The original value has to be restored into that particular text box/boxes and the cursor should focus set to that text box.

These are more or less the steps you should follow:

  1. Add the appropriate datasource (adapter, connection and dataset) to the page and bind it to a datagrid as usual.

  2. Add a templated column to it, with the textbox to edit the field. This would go in the ItemTemplate directly, as you don't mention the need for an explicit action to start editing. You can also add here a RangeValidator so that values are always of the appropriate type and are between your valid values. The new column would look like the following (I'm using Pubs database as an example):
    <asp:templatecolumn headertext="EditPrice">
      <asp:TextBox id="txtPrice" runat="server" 
     Text='<%# DataBinder.Eval(Container, "DataItem.price") %>'>
      <asp:rangevalidator id="valPrice" runat="server" 
     errormessage="Value must be a number less than 1000"
     minimumvalue="0" maximumvalue="1000" 
     controltovalidate="txtPrice" type="Double">

  3. Add a ValidationSummary control to accumulate errors from multiple rows.

  4. In the update action, you need to iterate all rows and apply changes. There's no way of tracking which rows have changed, so all of them must be updated. This is the code for the button click:
    private void Button1_Click(object sender, System.EventArgs e)
     if (Page.IsValid)
      DataSet ds = (DataSet) Session["data"];
      //Iterate and apply changes.
      foreach (DataGridItem item in DataGrid2.Items)
       //Get value from grid.
       double price = double.Parse(((TextBox) item.FindControl
       ds.Tables[0].Rows[item.ItemIndex]["price"] = price;

For simplicity, I stored the dataset in session state. The check for Page.IsValid is important, because it's the one that will tell you whether any validator is signaling an error.

Dig Deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.