3

I have a gridview that supports updating records. I have an edit template with a Dropdownlist(ddl) that replaces textbox. The DDL is bound to a datasource and I need to append a value (the current value of this field to the DDL). This enables users to select the current value as well as alternate values from the DDL.

The issue is that I need the DDl to be bound ('<%# Bind("Element") %>') so the update function works but I need to bind it after the current value of the field has been appended to the DDL which now occurs during the RowDataBound event.

In a nutshell; I need to get the current value of a field appended to the DDL before the Bind so my update works (else I get a DDL does not contain vale error). What is the earliest point/event where I can retrieve the value of a field (after the edit button is clicked) in a gridview so I can do some plumbing before the Binding takes place?

Help?

user937036
  • 341
  • 1
  • 3
  • 15

1 Answers1

0

Interesting question! You could handle the RowEditing event, which gets fired as the row is going into "Edit Mode" (when the Edit Button is clicked). Then just use the NewEditIndex property to find the row you're about to Edit.

So, something like this in your Code Behind:

protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
    // the row you're editing
    int rowToEdit = e.NewEditIndex; 

    // The numeric ordinal of your column where your DropDownList is.  I just picked 5 at random
    int ddlColumnIndex = 5; 

    // Get the DropDownList you're interested in modifying
    DropDownList myDDL = (DropDownList)myGridView.Rows[rowToEdit].Cells[ddlColumnIndex].FindControl("myDDL");

    // Do whatever processing you need to do here
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • @User937036: Glad I could help =) Feel free to click the empty "check mark" next to this answer to indicate that it worked for you.You can see how that works here: [Accept An Answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) – Josh Darnell Oct 07 '11 at 19:28