I jumped into a scenario, where I want to modify the value in a GridView cell, while clicking the UPDATE button of the GridView on-the-fly. Don’t start, let me explain the scenario.
By default, the Edit-Mode of a GridView row displays every cells in a TextBox. What if you want to display a DropDownList in the Edit-Mode of one cell & want to update the GridView row value with the SelectedValue from the DDL, while updating?
Well, Its not a brain-storming requirement. But an easy one!
In the <EditItemTemplate> of one of the cell, I placed a DropDownList. The DDL is populated with values from dbase. In the Edit-Mode, we can choose the values from DDL and when the Update button of GridViewRow is pressed, the SelectedValue of DDL should be updated along with the other values in the row.
protected void GridView1_ALLCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
foreach (DictionaryEntry entry in e.NewValues)
if (entry.Key.ToString() == "feedcategory")
DropDownList ddl = (DropDownList)GridView1_ALLCategory.Rows[e.RowIndex].Cells.FindControl("DropDownList2");
e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
What i tried to do is quite simple.
“The RowUpdating event is called when we press a row’s Update button, but before the GridView control updates the row”. So, what you’ve to do is to find the DDL control (DropDownList2) here, and replace the original value with your DDL’s SelectedValue.
Hopefully, next guess will be how will you find which cell (column) will be having the DDL. Its simple, each cell will be mapped with DataField value. (In the above case, my DataField value is feedcategory). All you’ve to do is, iterate through the Dictionary value until your matching DataField value is found. Once found, find the DDL using the FindControl(), & change the value.
Hope you get an entire picture.