views:

179

answers:

3

I would like to be able to bulk edit all the rows for one column using a GridView control. What is the best way to do this?

A: 

I guess u know this: http://msdn.microsoft.com/en-us/library/ms972948.aspx

iPhoneDev
Yes, thanks for the answer. That only allows you to edit one row at a time. I would like to edit all the rows for one column.
Joel Cunningham
+1  A: 

If you want to update all rows with same value then show proper control(textbox/dropdown/checkbox/radio) in column header else show the grid column in edit mode instead of label.

See following:
http://www.codeproject.com/KB/webforms/BulkEditGridView.aspx

Brij
The values need to be unique for each row.
Joel Cunningham
Edited answer, see the sample link.
Brij
I ended up using a similar approach to this control by inheriting from GridView and overriding the CreateRow event.
Joel Cunningham
A: 

Probably not the best, but an option is to set the primary key of your table as the DataKey of the GridView then iterate the grid and use the datakey and the edited value to update the DB. Here is an example.

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID">
    <Columns>.....


foreach (var item in GridView1.Items)
  {
    var id = (Guid)GridView1.DataKeys[item.DataItemIndex].Value;
    var txt= item.FindControl("AmountTextBox") as Textbox;

    if (cb != null && id.HasValue)
      UpdateRow(id.Value, txt.Text);
  }
alejandrobog