[ACCEPTED]-Gridview row editing - dynamic binding to a DropDownList-dynamic-binding

Accepted answer
Score: 27

Quite easy... You're doing it wrong, because 5 by that event the control is not there:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && 
        (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
    { 
        // Here you will get the Control you need like:
        DropDownList dl = (DropDownList)e.Row.FindControl("ddlPBXTypeNS");
    }
}

That 4 is, it will only be valid for a DataRow (the actually 3 row with data), and if it's in Edit mode... because 2 you only edit one row at a time. The e.Row.FindControl("ddlPBXTypeNS") will 1 only find the control that you want.

Score: 1

I am using a ListView instead of a GridView 5 in 3.5. When the user wants to edit I have 4 set the selected item of the dropdown to 3 the exising value of that column for the 2 record. I am able to access the dropdown 1 in the ItemDataBound event. Here's the code:

protected void listViewABC_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    // This stmt is used to execute the code only in case of edit 
    if (((ListView)(sender)).EditIndex != -1 && ((ListViewDataItem)(e.Item)).DisplayIndex == ((ListView)(sender)).EditIndex)
    {
        ((DropDownList)(e.Item.FindControl("ddlXType"))).SelectedValue = ((MyClass)((ListViewDataItem)e.Item).DataItem).XTypeId.ToString();
        ((DropDownList)(e.Item.FindControl("ddlIType"))).SelectedValue = ((MyClass)((ListViewDataItem)e.Item).DataItem).ITypeId.ToString();
    }
}
Score: 1

You can use SelectedValue:

<EditItemTemplate>
    <asp:DropDownList ID="ddlPBXTypeNS"
                      runat="server"
                      Width="200px"
                      DataSourceID="YDS"
                      DataTextField="CaptionValue"
                      DataValueField="OID"
                      SelectedValue='<%# Bind("YourForeignKey") %>' />
    <asp:YourDataSource ID="YDS" ...../>
</EditItemTemplate>

0

Score: 1
protected void grvSecondaryLocations_RowEditing(object sender, GridViewEditEventArgs e)  
{  
    grvSecondaryLocations.EditIndex = e.NewEditIndex;  

    DropDownList ddlPbx = (DropDownList)(grvSecondaryLocations.Rows[grvSecondaryLocations.EditIndex].FindControl("ddlPBXTypeNS"));
    if (ddlPbx != null)  
    {  
        ddlPbx.DataSource = _pbxTypes;  
        ddlPbx.DataBind();  
    }  

    .... (more stuff)  
}

0

Score: 0

The checked answer from balexandre works great. But, it will 12 create a problem if adapted to some other 11 situations.

I used it to change the value 10 of two label controls - lblEditModifiedBy and lblEditModifiedOn - when I was 9 editing a row, so that the correct ModifiedBy 8 and ModifiedOn would be saved to the db on 'Update'.

When 7 I clicked the 'Update' button, in the RowUpdating event 6 it showed the new values I entered in the 5 OldValues list. I needed the true "old values" as 4 Original_ values when updating the database. (There's 3 an ObjectDataSource attached to the GridView.)

The fix to this is 2 using balexandre's code, but in a modified 1 form in the gv_DataBound event:

protected void gv_DataBound(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gv.Rows)
    {
        if (gvr.RowType == DataControlRowType.DataRow && (gvr.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
        {
            // Here you will get the Control you need like:
            ((Label)gvr.FindControl("lblEditModifiedBy")).Text = Page.User.Identity.Name;
            ((Label)gvr.FindControl("lblEditModifiedOn")).Text = DateTime.Now.ToString();
        }
    }
}

More Related questions