简体   繁体   中英

Grid view Dropdown list data binding error

Am using the below code to bind the dropdown data from another table. And also refer that control name using rowindex. But it always return null.And also return the error message.

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

Am using the two method, but both return the control name null

First code:

 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();
            }

        }

    } 

Second :

 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();
    } 
}

Please help me to do this..

   <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>

The DropDown "DDL_STATUS_FT" is in Footer Template ..You must check it as follow..

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

EDITED2

you must have dropdownlist in aspx code in gridview item-template

you are finding using e.row.findcontrol without declaring it so abusively it return null

so, fist add dropdownlist into your gridview here is a sample of your dropdownlist

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

if you got null ctrl Control ctrl = e.Row.FindControl("DDL_STATUS_FT"); //It always return null

then make sure in your aspx code DDL_STATUS_FT Control is runat="server"

尝试通过cell和cellIndex查找控件,例如...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM