Edit all rows for one column in GridView

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

+6
gridview
source share
3 answers

If you want to update all rows with the same value, then display the correct control (text field / drop-down menu / checkbox / radio) in the column header, show the grid column in edit mode instead of label.

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

+3
source share
0
source share

This is probably not the best, but the option is to set the primary key of your table as a DataKey GridView, then iterate over the grid and use the datakey value and the edited value to update the database. 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); } 
0
source share

All Articles