how to find control in edit item template?

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

Leave a Comment