网格视图下拉列表数据绑定错误

时间:2012-09-25 06:40:15

标签: c# asp.net c#-4.0 data-binding gridview

使用以下代码绑定来自另一个表的下拉数据。并使用rowindex引用该控件名称。但它总是返回null。并返回错误消息。

  `Object reference not set to an instance of an object.` 

使用这两种方法,但都返回控件名称null

第一个代码:

 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
                DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
                dd.DataTextField = "fld_TName";
                dd.DataValueField = "fld_id";
                dd.DataSource = DS;
                dd.DataBind();
            }

        }

    } 

第二:

 In databind function



if (DS.Rows.Count > 0)
    {
        GridView2.DataSource = DS;
        GridView2.DataBind();

    foreach (GridViewRow grdRow in GridView2.Rows)
    {
        DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA1 = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
        DataSet7.sp_getall_trv_masterDataTable DS1 = TA1.GetData();
        // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.  
        DropDownList drdList = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[4].FindControl("DDL_STATUS_FT"));//It always return null

        // DataBinding of nested DropDownList Control for each row of GridView Control.  
        drdList.DataSource = DS1;
        drdList.DataValueField = "fld_id";
        drdList.DataTextField = "fld_TName";
        drdList.DataBind();
    } 
}

请帮我这样做..

   <asp:TemplateField ItemStyle-Width="100px" HeaderText="TYPE">
                        <ItemTemplate>
                            <asp:DropDownList ID="DDL_STATUS" runat="server" AutoPostBack="true" Enabled="false" >
                            </asp:DropDownList>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="DDL_edit_STATUS" runat="server" AutoPostBack="true" SelectedValue='<%# Eval("fld_Type") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:DropDownList ID="DDL_STATUS_FT" runat="server" AutoPostBack="true">
                            </asp:DropDownList>
                        </FooterTemplate>
                    </asp:TemplateField>

3 个答案:

答案 0 :(得分:1)

DropDown“DDL_STATUS_FT”位于Footer Template ..您必须按以下方式检查..

if(e.Row.RowType == DataControlRowType.Footer)
{
  DropDownList ctrl =(DropDownList)e.Row.Cells[CellIndex].FindControl("DDL_STATUS_FT"); 
}

答案 1 :(得分:0)

<强> EDITED2

你必须在gridview item-template

的aspx代码中有dropdownlist

你发现使用e.row.findcontrol而没有如此滥用地声明它返回null

所以,请先将dropdownlist添加到gridview中 这是您的下拉列表的示例

   <asp:TemplateField ItemStyle-Width="30px" HeaderText="DDL_STATUS_FT">
                        <ItemTemplate>
                            <asp:Dropdownlist ID="DDL_STATUS_FT" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>

如果你有空ctrl 控制ctrl = e.Row.FindControl(“DDL_STATUS_FT”); //它总是返回null

然后确保在您的aspx代码中DDL_STATUS_FT控件是runat="server"

答案 2 :(得分:0)

尝试通过cell和cellIndex找到您的控件,如...

Control ctrl = e.Row.Cells[yourCellIndex].FindControl("DDL_STATUS_FT");