Reputation: 37
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();
}
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
Upvotes: 1
Views: 7763
Reputation: 4293
Simply add this to your event
protected void grdViewDetails_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e) {
grdViewDetails.EditIndex = e.NewEditIndex;
Page_Load(null, null);
}
That Page_Load(null,null) -Postback- loads your datagridview with the new EditIndex (Given you update your gridview on the page_load event)
Upvotes: 0
Reputation: 505
Recently, I found same problem. What I did to fix it was add Gridview1.DataBind()
outside if(!IsPostBack)
and in each event handler.
Upvotes: 0
Reputation: 17614
You need to call bind gridview
again in edit function GridView1_RowEditing
You need to modify your code as follows
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEditIndex = e.NewEditIndex;
//call the function here
populagridview1(ddl_slice.SelectedValue, dt_to_integer(Picker1.Text),
dt_to_integer(Picker2.Text), Txt_Year.Text);
Gridview1.DataBind();
}
Upvotes: 1
Reputation: 1
<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>//in this place give textbox control
</EditItemTemplate>
</asp:TemplateField>
in edititemtemplate, you are giving label instead of textbox. So please change label to textbox in edititemtemplate and see it will work.
Upvotes: 0
Reputation: 10105
You are missing the !Page.IsPostBack
condition in your page load.
Upvotes: -1
Reputation: 4296
Unfortunately, I can't give you a whole answer, but I think this may point you in the right direction. Usually, when I see something like this, it has to do with the Page Lifecycle. Take a good look at the order in which your events are firing. It may be that your edit button only works on the second try due to the timing of the data binding relative to the event firing.
Upvotes: 0