title: Binding dropdownlist inside gridview edititemtemplate
body: <p>I'm not able to bind my dropdownlist present in edititem template . I am getting null reference when i try to access it.</p>
  
  <p>My design:  </p>
  
  <pre><code>&lt;asp:TemplateField HeaderText ="Category"&gt;
      &lt;ItemTemplate &gt;
      &lt;asp:Label ID="drpcategory" Text ='&lt;%#Bind("category") %&gt;' runat ="server" /&gt;
      &lt;/ItemTemplate&gt;
      &lt;EditItemTemplate&gt;
          &lt;asp:DropDownList ID="drpcategory1"  AppendDataBoundItems="True" runat="server" &gt;
          &lt;/asp:DropDownList&gt;
      &lt;/EditItemTemplate&gt;
  &lt;/asp:TemplateField&gt; 
  </code></pre>
  
  <p>My code behind: </p>
  
  <pre><code>protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
  {
      gv_table1.EditIndex = e.NewEditIndex;
      DropDownList drpcategory1 = ((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1"));
      //BindDropDown(drpcategory1);
      dt = con.GetData("Select category_name from category");
  
      String str = gv_table1.Rows[e.NewEditIndex].FindControl("drpcategory1").GetType().ToString();
      //((DropDownList)gv_table1.Rows[e.NewEditIndex].Cells[8].FindControl("drpcategory1")).DataSource = dt;
      drpcategory1.DataSource = dt;
      drpcategory1.DataTextField = "category_name";
      drpcategory1.DataValueField = "category_name";
      drpcategory1.DataBind();
  
      this.setgrid();
  }
  </code></pre>
  
  <p>I've tried looking on the net and tried many things in vain. I am new to asp. Thanks in advance. I would like the dropdown to be bound only when user enters edit mode.</p>