0

I have the following problem and i dont know why this is happening. I am declaring a dropdownlist in a grid view:

<asp:GridView ID="grdMappingList" runat="server" OnPageIndexChanging="grdMappingList_PageIndexChanging"  AutoGenerateColumns="false" AllowPaging="true" PageSize="10"  ShowHeaderWhenEmpty="true" UseAccessibleHeader="true" CssClass="table table-hover table-striped segment_list_tbl" >
                            <PagerStyle CssClass="grid_pagination" HorizontalAlign="Right" VerticalAlign="Middle" BackColor="#DFDFDF" />
                            <Columns>
                                <asp:BoundField HeaderText="Mapping Id" DataField="MAPPING_ID"></asp:BoundField>  
                                <asp:TemplateField HeaderText="Mapping Status" HeaderStyle-CssClass="width_120 aligned_center" ItemStyle-CssClass="width_120 aligned_center" Visible="true">
                                        <ItemTemplate>
                                            <asp:DropDownList id="ddgvOpp" runat="server" >
                                                <asp:ListItem Text="DONE"  Value="True"></asp:ListItem>
                                                <asp:ListItem Text="NOT DONE"  Value="False"></asp:ListItem>
                                                </asp:DropDownList>
                                        </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>

The above is my aspx code. The dropdownlist with the id="ddgvOpp" is not recognized in my cs code.

    private void InitializeDepartmentsDDL()
{
    List<ReplaceParameter> param = new List<ReplaceParameter>();
    //new- project selection
    param.Add(new ReplaceParameter("PROJECT_ID", Convert.ToInt32(User_Data["SelectedProj"].Rows[0]["ProjectID"].ToString())));

    DataTable AvailableDepartments = (DataTable)DQManager.Execute("LoadDepartments", null, param);
    ddgvOpp.DataSource = AvailableDepartments;
    ddgvOpp.DataTextField = "Text";
    ddgvOpp.DataValueField = "Value";
    ddgvOpp.DataBind();
    ddgvOpp.Items.Insert(0, new ListItem("Please Select...", "-1"));

}

The error that i am getting is that the ename ddgvOpp does not exist in the current context. Please bear in mind that if get the dropdown list outside the the gridview everything is working.

Any help please?

The above problem is solved. Now i am facing the following problem. So here is my code :

<asp:DropDownList id="ddlMappingStatus" runat="server"  SelectedValue='<%# Bind("MappingCompleted") %>'     OnSelectedIndexChanged="ddlMappingStatus_SelectedIndexChanged" AutoPostBack="true">
    <asp:ListItem Text="Done"  Value="DONE"></asp:ListItem>
    <asp:ListItem Text="Not Done"  Value="NOT DONE"></asp:ListItem>
</asp:DropDownList>

When i call ddlMappingStatus_SelectedIndexChanged in order to get all the attributes of the specific row nothing is returned. I can't get the values of the specific row and also the DropDownList ddgvOpp = (DropDownList)grdMappingList.SelectedRow.FindControl("ddlMappingStatus"); is giving an exception of null object

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
apoellitsi
  • 249
  • 6
  • 21

1 Answers1

3

You have to use GridViewRow.FindControl("ControlID") if you want to get the reference to the control in a TemplateField of a GridView since the row is the NamingContainer not the page.

if(grdMappingList.SelectedRow != null)
{
    DropDownList ddgvOpp = (DropDownList) grdMappingList.SelectedRow.FindControl("ddgvOpp");
    // ...
}

This presumes that you want to find the DropDownList of the selected row of the GridView. If you want to find all you have to loop the rows:

foreach(GridViewRow row in grdMappingList.Rows)
{
     DropDownList ddgvOpp = (DropDownList) row.FindControl("ddgvOpp");
     // ...
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • now its working but i am facing another issue now. the dropdownlist does not supporting OnSelectedIndexChanged?? I am declaring that and it fails – apoellitsi Feb 15 '16 at 15:04
  • @apoellitsi: what means "it fails"? You have to set `AutoPostBack` tro `true`(default is `false`). Maybe you're also databinding the gridview on every postback. That prevents events. Check it with `if(!IsPostBack)DataBindGrid();` – Tim Schmelter Feb 15 '16 at 15:04
  • actually my mistake is that i cant get the values of the specific row that i want to change the value of the dropdown list. For example i am trying to get the Mapping Id field and i use that grdMappingList.SelectedRow.Cells[0].Text and is coming as null. – apoellitsi Feb 15 '16 at 15:23
  • even for this DropDownList ddgvOpp = (DropDownList)grdMappingList.SelectedRow.FindControl("ddlMappingStatus"); i am getting an exception. "Object reference not set to an instance of an object" – apoellitsi Feb 15 '16 at 15:35
  • @apoellitsi: your aspx doesnt contain a control with ID=`ddlMappingStatus` why dont you use `FindControl("ddgvOpp")`? – Tim Schmelter Feb 15 '16 at 15:42
  • i have change the name and i rename it as ddlMappingStatus :) – apoellitsi Feb 15 '16 at 15:44
  • As i understandis when i select a specific dropdown list for a specific row it doesn't recognise the attributes of the row. – apoellitsi Feb 15 '16 at 15:46
  • I have edited my question in order to understand what is the error that i am getting. – apoellitsi Feb 15 '16 at 15:52
  • @apoellitsi: are you sure that this error is related to your first, you said that it's solved. If it's a new issue you should ask a new question, otherwise it's confusing and not helpful for future readers. – Tim Schmelter Feb 15 '16 at 16:12
  • okay i have done what you said. The question is posted here : [link]http://stackoverflow.com/questions/35416470/retrieve-values-from-asp-gridview-by-changing-a-value-in-a-dropdownlist. – apoellitsi Feb 15 '16 at 18:29