Click edit button twice in gridview asp.net c# issue

Posted by Supriyo Banerjee on Stack Overflow See other posts from Stack Overflow or by Supriyo Banerjee
Published on 2012-04-03T14:34:46Z Indexed on 2013/06/28 4:21 UTC
Read the original article Hit count: 344

Filed under:
|

I have a gridview created on a page where I want to provide an edit button for the user to click in. However the issue is the grid view row becomes editable only while clicking the edit button second time. Not sure what is going wrong here, any help would be appreciated.

One additional point is my grid view is displayed on the page only on a click of a button and is not there on page_load event hence.

Posting the code snippets:

//MY Aspx code

        <Columns>

            <asp:TemplateField HeaderText="Slice" SortExpression="name">
                <ItemTemplate>
                    <asp:Label ID="lblslice" Text='<%# Eval("slice") %>' runat="server"></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblslice" Text='<%# Eval("slice") %>' runat="server"></asp:Label>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Metric" SortExpression="Description">
                <ItemTemplate>
                <asp:Label ID="lblmetric" Text='<%# Eval("metric")%>' runat="server"></asp:Label>
                    </ItemTemplate>
                     <EditItemTemplate>
                <asp:Label ID="lblmetric" Text='<%# Eval("metric")%>' runat="server"></asp:Label>
                    </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Original" SortExpression="Type">
                <ItemTemplate>
                                        <asp:Label ID="lbloriginal" Text='<%# Eval("Original")%>' runat="server"></asp:Label>
                    </ItemTemplate>
                      <EditItemTemplate>
                                        <asp:Label ID="lbloriginal" Text='<%# Eval("Original")%>' runat="server"></asp:Label>
                    </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="WOW" SortExpression="Market">
                <ItemTemplate>
                                        <asp:Label ID="lblwow" Text='<%# Eval("WOW")%>' runat="server"></asp:Label>

                    </ItemTemplate>
                    <EditItemTemplate>
                                        <asp:Label ID="lblwow" Text='<%# Eval("WOW")%>' runat="server"></asp:Label>

                    </EditItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Change" SortExpression="Market" >
                <ItemTemplate>
                <asp:Label ID="lblChange" Text='<%# Eval("Change")%>' runat="server"></asp:Label>

                    </ItemTemplate>
                     <EditItemTemplate>
                    <asp:TextBox ID="TxtCustomerID" Text='<%# Eval("Change") %> ' runat="server"></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField HeaderText="Edit" ShowEditButton="True" />

        </Columns>
    </asp:GridView>

//My code behind:

protected void Page_Load(object sender, EventArgs e)
{



}

public void populagridview1(string slice,string fromdate,string todate,string year)
{
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "usp_geteventchanges";
    cmd.Connection = conn;
    conn.Open();
    SqlParameter param1 = new SqlParameter("@slice", slice);
    cmd.Parameters.Add(param1);
    SqlParameter param2 = new SqlParameter("@fromdate", fromdate);
    cmd.Parameters.Add(param2);
    SqlParameter param3 = new SqlParameter("@todate", todate);
    cmd.Parameters.Add(param3);
    SqlParameter param4 = new SqlParameter("@year", year);
    cmd.Parameters.Add(param4);


    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds, "Table");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    conn.Close();
}

protected void ImpactCalc(object sender, EventArgs e)
{
    populagridview1(ddl_slice.SelectedValue, dt_to_integer(Picker1.Text),        dt_to_integer(Picker2.Text), Txt_Year.Text);

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{


    gvEditIndex = e.NewEditIndex;
    Gridview1.DataBind();


}

My page layout

This edit screen appears after clicking edit twice.. the grid view gets displayed on hitting the Calculate impact button. The data is from a backend stored procedure which is fired on clicking the Calculate impact button

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about datagridview