Saturday, August 8, 2009

Changing Multiple GridView Row values using RowUpdating event

This is an extended sample of my previous post titled Change GridView Row values using RowUpdating Event. Recently, I got comment from NobleMule asking how he can edit multiple column/cell values.

I have 4 DDL's in a gridview. The first one is updating (mostly thanks to your code) but I can't get the other three to update. I'm a fairly new to C# and have never used the DictionaryEntry before. I naively tried just adding more if's (one for each ddl) within the foreach loop but that didn't work. Any help would be greatly appreciated.

I hope the 4 DDL’s are in 4 different Cells of a GridView. And wants to update the new values selected in those DDL’s into the database. As he’d mentioned my previous post sample is only meant for updating the DDL present in one Cell of your GridView.

I’ll break-up the concept here, so that you can deal with any number of Cells in the GridView. These are the tips to be kept in mind before start coding.

#1. Identify the Row in the GridView for updating, obtained via e.RowIndex

#2. Identify the Cell (column) in the GridView for updating.
     This value will be entered manually. Keep in mind, Cell indexing starts from zero.

#3. Locate the control (DDL here) using FindControl() method and cast it to appropriate type (ie, DDL type here).

These tips help you to find a Control in a particular (row,cell) of a GridView.

Now, the real case. You want to update the DDL in 4 different Cells of a GridView. All you’ve to do is to:
(a) Identify each Cell of GridView
(b) Access the Control (DDL here), from each Cell
(c) Get the Selected Value from DDL
(d) Update with new value

Lets assume that we’ve a Table having cells as:

IdColumn1Column2Column3Column4

And, Column1 having DropDownList1, Column2 having DropDownList2, Column3 having DropDownList3 and Column4 having DropDownList4.

Now, the rough piece of code looks like:

   1:  
   2: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   3:     {
   4:         foreach (DictionaryEntry entry in e.NewValues)
   5:         {
   6:             DropDownList ddl;
   7:  
   8:             if (entry.Key.ToString() == "Column1")
   9:             {
  10:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1");
  11:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  12:             }
  13:             if (entry.Key.ToString() == "Column2")
  14:             {
  15:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[2].FindControl("DropDownList2");
  16:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  17:             }
  18:             if (entry.Key.ToString() == "Column3")
  19:             {
  20:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("DropDownList3");
  21:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  22:             }
  23:             if (entry.Key.ToString() == "Column4")
  24:             {
  25:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList4");
  26:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  27:             }
  28:         }
  29:     }

Hope this makes some sense.


Thanks.

4 comments:

Anonymous said...

great code example of how to change the e.NewValues collection. This helped me out a lot thanks!

Anonymous said...

Works great for me. Thanks for posting the tips.

JimOG said...

You can also refer to the column by name using:
GridViewRow row = gvEpayVendorSetup.Rows[e.RowIndex];
string dtval = dt.Rows[row.DataItemIndex]["STATUS"]:

Abhilash said...

Hello JimOG,
Thanks for pointing our another trick. :)

 
Best viewed in Internet Explorer 8.