Take the 2-minute tour ×

I'm following this example http://www.codeproject.com/KB/webforms/Editable_GridView.aspx to build an editable GridView control. I have this code in my GridView:

<asp:TemplateField HeaderText="Negócio">
    <asp:Label ID="lblNegocio" runat="server" Text='<%# Eval("Negocio") %>'></asp:Label> 
    <asp:DropDownList ID="ddlNegocio" runat="server" /> 
    <asp:DropDownList ID="ddlNewNegocio" runat="server" />

Now, I'm trying to fill the dropdown in the EditItemTemplate with some dynamic values just as the example says, in the RowDataBound Event of the grid. But when I do this, the FindControl method always returns Nothing:

Protected Sub gdvRegraRotationDefault_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdvRegraRotationDefault.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
    Dim ddlNegocio As DropDownList = e.Row.FindControl("ddlNegocio")
End If

End Sub

If I can't find the Dropdown I can't load the values in it and when I'm going to edit de entry it will be empty.

Can someone help me?

Thank you (:

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Please use the RowEditing-Event, as your DropDownList should only be shown when clicking Edit. But first, you have to bind the GridView newly as the GridView now needs to render different controls for the edit row:

protected void gdvRegraRotationDefault_RowEditing(object sender, GridViewEditEventArgs e)
    gdvRegraRotationDefault.EditIndex = e.NewEditIndex;

    GridViewRow row = gdvRegraRotationDefault.Rows[e.NewEditIndex];
    DropDownList ddl = row.FindControl("ddlNegocio") as DropDownList;

    //now do databinding for DropDownList
share|improve this answer
Thank you very much. (: –  bigodera Dec 20 '11 at 10:48
You´re welcome! –  AGuyCalledGerald Dec 20 '11 at 10:50

The FindControl always return null because when your in the RowDataBound event you can get the label only.

If you want to fill the DropDownList when you click the edit button on the grid, then you have to use the GridViewRowEditing event.

share|improve this answer
Right, but in this event I can't use the FindControl method because the class GridViewEditEventArgs has no access to the Row property. How should I do it? –  bigodera Dec 20 '11 at 9:54
You have tow choices, either parse the sender to a GridViewRow or use the e.NewEditIndex which is the index of the Row being edited, then you find the control in that row. –  yahya kh Dec 20 '11 at 9:59
Dim row As GridViewRow = gdvRegraRotationDefault.Rows(e.NewEditIndex) Dim ddlNegocio As DropDownList = row.FindControl("ddlNegocio") //Still returns Nothing ): –  bigodera Dec 20 '11 at 10:13
As Jan-Frederik Carl said, you have to do a GridView.DataBind(), after that you can access your control. –  yahya kh Dec 20 '11 at 10:37
Thanks man! Have a great day. (: –  bigodera Dec 20 '11 at 10:48

In the RowDataBound event, simply add the following conditional:

if (myGridView.EditIndex == e.Row.RowIndex)
     //do work
share|improve this answer
Hmmm I think you didn't understand my problem man, that won't help me. Thanks anyway. –  bigodera Dec 20 '11 at 10:36
you are not able to find your dropdown control right –  Neha Dec 20 '11 at 10:38
Yes, but Jan-Frederik Carl and yahya kh already helped me solve my problem. Thank you. –  bigodera Dec 20 '11 at 10:50

Your Answer


