0

My TT.aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" >
    <Columns>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal ID="lit1" Text='<%#Eval("E_Name")%>' runat="server">

            </asp:Literal>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="Eq" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Name">

            </asp:DropDownList>

        </EditItemTemplate>

        </asp:TemplateField>

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [E_Name], [Problem], [Solution] FROM [Equipment] WHERE ([O_ID] = @O_ID)">
    <SelectParameters>
        <asp:QueryStringParameter Name="O_ID" QueryStringField="TT" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name] FROM [Parts]">
</asp:SqlDataSource>

My TT.aspx.cs

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Literal xx = (Literal)GridView1.Rows[e.NewEditIndex].FindControl("lit1");
        String x =  xx.Text;
        DropDownList x1 = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("Eq");
        x1.SelectedValue = x;
    }

I'm a newbie to asp.net and I'm only testing with this code. The problem I have is that the 2nd FindControl("Eq") always returns null while the first one (for the Literal) returns the correct value. I tried setting the edit index of the gridview to e.NewEditIndex but doesn't seem to work.

Basically what I want to happen is if the user clicks on Edit on a row, the original databound value, which is in the label, is selected in the dropdownlist.

Can anybody guide me through this, please?

Mohammad
  • 1
  • 1
  • 3
  • check this http://stackoverflow.com/questions/12247279/binding-dropdownlist-inside-gridview-edititemtemplate – Waqas Raja Jun 03 '13 at 11:11
  • I have already checked it out before asking, but I don't see how it relates. I say this because he is implementing the code in the `RowDataBound` method while I'm implementing it in the `RowEditing` method. Also I already tried `GridView1.EditIndex = e.NewEditIndex;` but it didn't work. Correct me if I'm wrong, please. – Mohammad Jun 03 '13 at 11:16

1 Answers1

0

The RowEditing event just gives the row index being edited. You need to set the edit row index to the grid and rebind it. And if you need to set a value or need to bind a control inside EditItemTemplate you have to do it in RowDateBound event, as you will be re-binding grid in RowEditing event so the RowDataBound event will also give you the EditItemTemplate's controls.

As shown in this answer

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
  // grid view's edit index has been changed so rebind it
  gv.EditIndex = e.NewEditIndex;
}

protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
  {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
          ddList.SelectedValue = "set your value here";
        }
   }
}
Community
  • 1
  • 1
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • +1 but you only need to set the EditIndex if you are manually binding the GridView using the DataSource property and DataBind method. This answer would work without the RowEditing event re-binding the GridView. – Sean Airey Jun 03 '13 at 11:30
  • I'll try it now but quick question; will clicking the edit button invoke the `RowDataBound` method? If not, then how can I invoke it programatically from the `RowEditing` method? – Mohammad Jun 03 '13 at 11:34
  • Pressing edit button will invoke `RowEditing` event and in row editing event upon rebinding of grid the `RowDateBound` event will fire, as @Sean pointed out you are using `DataSource` property so you will not need to rebind the grid just set `EditIndex` the `RowDataBound` will be fired automatically. – Waqas Raja Jun 03 '13 at 11:41
  • Now `Literal xx = (Literal)e.Row.FindControl("lit1");` is returning null. Is this the right way to reference "lit1" from the `RowDataBound` method? – Mohammad Jun 03 '13 at 11:55
  • I managed a workaround solution, a bit messy but does the job. I'm saving the text inside the label in a global variable, once I read it in the `RowEditing` method, and then read it in the `RowDataBound` method – Mohammad Jun 03 '13 at 12:05