krishna mohan
krishna mohan

Reputation: 921

unable to update gridview values in .net

while updating gridview record . old values only getting updated. im using bound field. getting old value while im fetching data in variable.

<asp:GridView runat="server" ID="GvLeads" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" OnRowDeleting="GvLeads_RowDeleting" OnRowEditing="GvLeads_RowEditing" OnRowCancelingEdit="GvLeads_RowCancelingEdit" EmptyDataText="No Records Found" OnRowUpdating="GvLeads_RowUpdating" OnRowDeleted="GvLeads_RowDeleted">
 <Columns>
    <asp:BoundField HeaderText="Id" DataField="LeadId" />
    <asp:BoundField HeaderText="Company" DataField="Companyname" />
 </Columns>
</GridView >

code behind

protected void GvLeads_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridViewRow row = GvLeads.Rows[e.RowIndex];
    String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
    int Leadid = Convert.ToInt32(str);
    string CompanyName = ((TextBox)(row.Cells[2].Controls[0])).Text;
 }

Upvotes: 1

Views: 226

Answers (1)

Suprabhat Biswal
Suprabhat Biswal

Reputation: 3216

This usually happens when you are populating grid at Page_Load as soon as RowUpdating event gets called before that Page_Load event get's called which populates the grid with initial values. How to Avoid? Use !IsPostBack for this purpose

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid(); // For e.g.
    }
}

Upvotes: 1

Related Questions