MrProgram
MrProgram

Reputation: 5242

Can't find dropdownlist inside gridview

I'm trying to bind a dropdownlist to a dataset but I get null value at my DDL. When I click at a row for EDIT mode the DDL won't bind because I can't find it. Why won't I find the rows dropdownlists and bind them??

using (var scTyche = new SqlConnection(ConfigurationManager.ConnectionStrings["KondorConnectionConnectionString"].ConnectionString))
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            var ddl = (DropDownList)row.FindControl("DropDownListFolders1");
            var da = new SqlDataAdapter();
            var dt = new DataTable();
            var ds = new DataSet();

            scTyche.Open();
            var cmdTyche = scTyche.CreateCommand();
            cmdTyche.CommandType = CommandType.StoredProcedure;
            cmdTyche.CommandText = "dbo.spGetFolders";
            cmdTyche.CommandTimeout = 60;
            cmdTyche.Parameters.Add("@nBranchId", SqlDbType.Int).Value = intBranchId;

            da.SelectCommand = cmdTyche;
            da.Fill(dt);
            ds.Tables.Add(dt);

            ddl.DataSource = ds;
            ddl.DataTextField = "strShort";
            ddl.DataValueField = "nId";
            ddl.DataBind();

            cmdTyche.Parameters.Clear();

            scTyche.Dispose();
            scTyche.Close();
        }
    }

My aspx:

<asp:UpdatePanel runat="server" ID="update">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
            AllowSorting="True" AutoGenerateColumns="False" SkinID="gridviewGridlinesSkin"
            OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting"
            EmptyDataText="No positions found">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                         <asp:Label ID="Label4" runat="server" Text='<%# Eval("strPositionId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Folder" SortExpression="strFolderName">
                    <EditItemTemplate>
                        <BrummerComp:SortableDropDownList ID="DropDownListFolders1" Width="141px" runat="server"
                                SkinID="BCdropdownlistSkin"/>
                    </EditItemTemplate>
</asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Views: 746

Answers (1)

Nick Niebling
Nick Niebling

Reputation: 317

If you want to bind the drop down manually from code behind, I would suggest you do this either in the GridView.OnDataBinding, GridView.OnDataBoung, GridView.OnRowDataBound events or DropDown.OnDataBinding, DropDown.OnDataBound data binding events.

Right now, you might be trying to data bind at a wrong timing - maybe before a row is changed to edit mode?

Also remember only 1 row would be in edit mode - if you use GridView.OnRowDataBound, you can use the event args (e.Row.RowState == DataControlRowState.Edit) to determine if the current event is handling a row in edit mode or not. This will reduce overhead in your code and make it easier to debug (potentially isolating the issue).

Upvotes: 1

Related Questions