11

i have a gridview on the form and have some template field, one of them is:

<asp:TemplateField HeaderText="Country" HeaderStyle-HorizontalAlign="Left">
    <EditItemTemplate>
        <asp:DropDownList ID="DdlCountry" runat="server" DataTextField="Country" DataValueField="Sno">
        </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateField>

now on the RowEditing event i need to get the selected value of dropdownlist of country and then i will set that value as Ddlcountry.selectedvalue=value; so that when dropdownlist of edit item template appears it will show the selected value not the 0 index of dropdownlist. but i am unable to get the value of dropdown list. i have tried this already:

int index = e.NewEditIndex;
DropDownList DdlCountry = GridView1.Rows[index].FindControl("DdlCountry") as DropDownList;

need help please. thanx.

Mogli
  • 1,972
  • 11
  • 34
  • 67

3 Answers3

19

You need to databind the GridView again to be able to access the control in the EditItemTemplate. So try this:

int index = e.NewEditIndex;
DataBindGridView();  // this is a method which assigns the DataSource and calls GridView1.DataBind()
DropDownList DdlCountry = GridView1.Rows[index].FindControl("DdlCountry") as DropDownList;

But instead i would use RowDataBound for this, otherwise you're duplicating code:

protected void gridView1_RowDataBound(object sender, GridViewEditEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
          DropDownList DdlCountry = (DropDownList)e.Row.FindControl("DdlCountry");
          // bind DropDown manually
          DdlCountry.DataSource = GetCountryDataSource();
          DdlCountry.DataTextField = "country_name";
          DdlCountry.DataValueField = "country_id";
          DdlCountry.DataBind();

          DataRowView dr = e.Row.DataItem as DataRowView;
          Ddlcountry.SelectedValue = value; // you can use e.Row.DataItem to get the value
        }
   }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • finally got the reference of dropdown :) but i adopt a different way to assign the selected value to dropdown.i use datakey property of gridview.thnx a lot Mr. Tim :) – Mogli Jan 30 '13 at 10:42
  • 1
    is there any way, that i dont have to hit de DB again, but bind de dropdownlist when the edit fires – Sandeep Apr 01 '13 at 11:37
  • The first example did not work for me, but the RowDataBound did. Thank you – Fandango68 Feb 08 '16 at 00:46
6

You can try wit this code - based on EditIndex property

var DdlCountry  = GridView1.Rows[GridView1.EditIndex].FindControl("DdlCountry") as DropDownList;

Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.editindex.aspx

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • 1
    unable to find the ddlcountry control, it shows the null in DdlCountry variable ? – Mogli Jan 30 '13 at 06:44
  • Yeah that doesn't work either. It seems to be creating a new instance of ddlcountry and not a reference to the one in edit mode – Fandango68 Feb 08 '16 at 00:34
  • This works particularly well outside of a GridView event handle method (e.g. in a ModelBinding method). – secretwep Oct 13 '16 at 21:17
2

I looked at the controls in scope while debugging this issue for selecting a drop down item in a DropDownList in an EditItemTemplate. The issue here, the data is not part of the HTML in the initial form and when the OnRowEditing posts back, there is no information about those edit controls.

If you use the F12 developer tools on your browser page before the OnRowEditing you will not see the editing control as ASP did not written them to the HTML with the initial data bind; this keeps the form data as small as possible. After OnRowEditing, the return will show the editing controls as ASP has now written them to the HTML. The solution I went with was:

  1. Get the 'selection' information from the current GridView which held the initial data bind and store it in a local string.
  2. Overwrite the GridView with the new 'Editing' data with a new data bind.
  3. Get the DropDownList control and set it to the local 'selection' string.
protected void gv_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        try
        {
            gv.EditIndex = e.NewEditIndex; // row index being edited

            string businessSelection = (gv.Rows[e.NewEditIndex].FindControl("lblFieldName") as Label).Text;

            gv.DataSource = GetBusinessData(); gv.DataBind();

            (gv.Rows[e.NewEditIndex].FindControl("ddlFieldName") as DropDownList).SelectedValue = businessSelection;
        }
        catch (Exception ex) { ErrorHandleAndLog(ex); }
    }

The field markup looks like this (internal naming conventions and data values removed, of course):

<%-- Field Comment --%>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
  <HeaderTemplate>
    <asp:Label runat="server" Text="Field Name" />
  </HeaderTemplate>
  <ItemTemplate>
    <asp:Label runat="server" ID="lblFieldName" Text='<%# Eval("FieldName") %>' />
  </ItemTemplate>
  <EditItemTemplate>
    <asp:DropDownList runat="server" ID="ddlFieldName" >
      <asp:ListItem Text="" />
      <asp:ListItem Text="Business Option 1" />
      <asp:ListItem Text="Business Option 2" />
      <asp:ListItem Text="Business Option 3" />
    </asp:DropDownList>
  </EditItemTemplate>
  <FooterTemplate>
    <asp:DropDownList runat="server" ID="ddlNewFieldName">
      <asp:ListItem Text="" />
      <asp:ListItem Text="Business Option 1" />
      <asp:ListItem Text="Business Option 2" />
      <asp:ListItem Text="Business Option 3" />
    </asp:DropDownList>
  </FooterTemplate>
</asp:TemplateField>
Anthony
  • 21
  • 1