0

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>
1
  • where is your drop downlist in aspx code DDL_STATUS_FT ? Commented Sep 25, 2012 at 7:12

3 Answers 3

1

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"); 
}
Sign up to request clarification or add additional context in comments.

5 Comments

It's working fine..But i have one doubt i want to add this in itemtemplate also
DataControlRowType.DataRow is used to get Controls in ItemTemplate..You cannot find "DDL_STATUS_FT" if you are checking RowType as Datarow...
yes i find the items for all row. But i want to include the gridview row edit event.. protected void EditGrade(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; BindData1(); } //Error in Bind datasection.
private void BindData1() { DataSet7TableAdapters.sp_getall_trv_config_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_config_masterTableAdapter(); DataSet7.sp_getall_trv_config_masterDataTable DS = TA.GetData(); if (DS.Rows.Count > 0) { GridView2.DataSource = DS; GridView2.DataBind(); }}
@romi...this question was regarding object reference error..and i think it has been solved now...if you have any further issues or queries you can ask them creating new Questions.Formatting is not applied in comment area..so it is difficult to understand what u need.and also if you create new questions chances that u will get perfect solutions will be higher as everyone will be able to see it and help you out...
0

Try to find your control by cell and cellIndex like...

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

Comments

0

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"

3 Comments

DS not dataset, It's a datatable
if (ctrl != null) value always return null value
But My dropdown id is present in the gridview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.