Home » ASP.NET » ASP.NET GridView Edit All Rows At Once

ASP.NET GridView Edit All Rows At Once

I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go…

Here’s the problem.  You want to be able to edit all of the rows in the gridview at once instead of having to switch to edit mode and save one row at the time.  Normally, you’d want to do this when only a couple of items need to be changed per row and not the entire row’s worth of data.


Photo credit: tico_24 via VisualHunt.com / CC BY

You can do this easily if you make the columns that need to be edited templated columns and place editable controls in them (checkbox, textbox, etc)  You can then either make these controls "AutoPostback" controls, or you can provide a control at the bottom of the screen that triggers the update.  In either case, the code you are going to write at the codebehind level is going to be the same.

For this example, we are going to assume that you only have one column that needs to be updated and that the control is a checkbox control.

One of the issues you are going to run into with this is that you’ll need to know which row is associated with the control when it is updated.  The easiest way I’ve found of dealing with this problem is by adding a HiddenField control and databinding the row Id to it.  Since we are dealing with a CheckBox control, you will need to create an event handler for the Checked event.  The first parameter that will be passed into this event handler will be the sender.  Sender represents the control that fired the event.  In this case, it will represent the CheckBox control.

The other control you’ll need to retrieve is the HiddenField control that you placed next to the check box.  You can retrieve this control by using the FindControl() method that is hanging off the parent control of the check box.  Assuming your HiddenField control is named "_hiddenFieldId" you can get the ID by using:

string id = (HiddenField)(((CheckBox)(Sender)).Parent

Now that you have the value of the ID and the value of the checkbox, you can update your database.


Other post in ASP.NET

Related Post

  • GridViews – Multiple Rows Per RecordGridViews – Multiple Rows Per Record I don't know about you, but I've had several occasions where I've needed to use the simplicity of the GridView control in ASP.NET.  DataBinding, Paging, Sorting, etc.  But I've also […]
  • GridView and Updating A Row ManuallyGridView and Updating A Row Manually A couple of days ago I mentioned a project that I’ve been working on that is a bit out of the ordinary as far as GridViews go.  One of the issues I’ve had is that the edit template […]
  • Access a control by ID From Within a Databound ControlAccess a control by ID From Within a Databound Control Databound controls are at once very easy and very frustrating.  If you just need to do some simple databinding that gets a list of items on the screen and you need the ability to edit […]
  • FormView Edit and AddFormView Edit and Add The FormView generally makes editing a record at a time pretty easy.  However, one of the biggest problems I've seen with this control is that there is no way of telling the FormView to […]
  • Multi-Step Processing in ASP.NETMulti-Step Processing in ASP.NET I received the following question a few days ago but I’ve been so busy with billable work that I just haven’t had a chance to answer it until now.  Actually, I’m still busy, but I […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.