ASP.NET: GridView, Editing multiple Rows – Bulk Updates

Adding a Procedure to Perform Bulk Updates

After you have configured the GridView control to display editable data, you need to add code to perform the bulk update. In this section, you will do the following:
  • Add a Button control and in its Click handler, add code to bulk update the changes from each row of the GridView control.
  • Add a DataTable object that stores the original data values.
  • Add code to determine if a row has been modified. The current values displayed in the GridView control will be compared to the original values stored in theDataTable object. If one or more of the displayed values do not match the original value, the row will be updated in the database. Otherwise, the row will not be included in the bulk update.

To create a procedure for performing the bulk update

  1. Switch to Design view.
  2. Select the GridView control, and in the Properties window, click the Events button (Events Button in the Properties Window) to display the events for the GridView control.
  3. In the RowDataBound field, type GridView1_RowDataBound and press ENTER.
    Visual Web Developer creates an event handler for the RowDataBound event of the GridView control. The code will look like the following code example.
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
    }
    
    
  4. Replace the generated procedure with the following code (including the private variables).
    private bool tableCopied = false;
    private DataTable originalDataTable;
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (!tableCopied)
            {
                originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
                ViewState["originalValuesDataTable"] = originalDataTable;
                tableCopied = true;
            }
    }
    
    
    The code runs whenever the GridView control is performing data binding. While the first row is binding, the code saves a copy of the original database values in aDataTable object, which is in turn stored in ViewState.
  5. Switch to Design view.
  6. From the Standard tab in the Toolbox, drag a Button control onto the page.
  7. In the Properties window, click the Properties button (PropertySymbolButton screenshot) to display the properties for the Button control.
  8. In the (ID) field, enter UpdateButton.
  9. In the Text field, enter Update.
  10. Click the Events button (Events Button in the Properties Window) to display events for the Button control.
  11. In the Click field, type UpdateButton_Click and press ENTER.
    Visual Web Developer creates an event handler for the Button control’s Click event. The code will look like the following code example.
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
    
    }
    
    
  12. Replace the generated procedure with the following code.
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        originalDataTable = (DataTable)ViewState["originalValuesDataTable"];
    
        foreach (GridViewRow r in GridView1.Rows)
            if (IsRowModified(r)) { GridView1.UpdateRow(r.RowIndex, false); }
    
        // Rebind the Grid to repopulate the original values table.
        tableCopied = false;
        GridView1.DataBind();
    }
    
    protected bool IsRowModified(GridViewRow r)
    {
        int currentID;
        string currentTitleOfCourtesy;
        string currentLastName;
        string currentFirstName;
        string currentTitle;
        string currentExtension;
    
        currentID = Convert.ToInt32(GridView1.DataKeys[0].Value);
    
        currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text;
        currentLastName = ((TextBox)r.FindControl("LastNameTextBox")).Text;
        currentFirstName = ((TextBox)r.FindControl("FirstNameTextBox")).Text;
        currentTitle = ((TextBox)r.FindControl("TitleTextBox")).Text;
        currentExtension = ((TextBox)r.FindControl("ExtensionTextBox")).Text;
    
        DataRow row =
            originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0];
    
        if (!currentTitleOfCourtesy.Equals(row["TitleOfCourtesy"].ToString())) { return true; }
        if (!currentLastName.Equals(row["LastName"].ToString())) { return true; }
        if (!currentFirstName.Equals(row["FirstName"].ToString())) { return true; }
        if (!currentTitle.Equals(row["Title"].ToString())) { return true; }
        if (!currentExtension.Equals(row["Extension"].ToString())) { return true; }
    
        return false;
    }
    
    
    NoteNote
    The procedure performs a string comparison using the value in each editable TextBox control and the value stored in the cached DataTable object. If you have formatted the text in the TextBox control, the values might be equivalent, but their string representations will not match. For example, if you have formatted aDateTime value using the small date format ({0:d}), the value in the date TextBox control might be 3/2/2005. The string representation of the date value from the DataTable object would be 3/2/2005 12:00 AM. In these cases, you must add comparison logic that takes formats and localization settings into account.
    The procedure iterates through the rows of the GridView control and calls the custom IsRowModified function for each row. The function compares the current row to the corresponding row in the DataTable object, and returns true if the row has changed. For any rows that have changed, the code in the button’s Click handler calls the UpdateRow method of the GridView control.
Advertisements

One thought on “ASP.NET: GridView, Editing multiple Rows – Bulk Updates

  1. This is a pretty good tutorial. I think it would be a lot better and more helpful if you'd include some info on the gridview you're actually using.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s