Editing in a GridView without switching to Edit mode.

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.

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.
    FindControl(“_hiddenFieldId”)).Text;

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

del.icio.us Tags: ,

Technorati Tags: ,

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear