user1172910
user1172910

Reputation: 73

Gridview not retaining old values in c# and ASP.Net

I have an ASP.Net gridview which is binding properly.On edit i am editing certain fields.then after i need to edit other rows as well.But when i am trying to edit other rows the previously edited rows get reset n retain their older values.

The requirement is as such that i have to edit many rows and the on a button click i need to push all the edit values to the database

Upvotes: 4

Views: 2701

Answers (7)

user1172910
user1172910

Reputation: 73

protected void grdRepayment_RowEditing(object sender, GridViewEditEventArgs e)

{

    grdRepayment.EditIndex = e.NewEditIndex;
    myDatatable = (DataTable)Session["TempTable"];
    grdRepayment.DataSource = myDatatable;
    grdRepayment.DataBind();

}

This is how we need to Bind the grid in RowEditing and RowCancelingEdit

Upvotes: 0

user1193035
user1193035

Reputation:

Please check you'r gridview binding method was call page load and inside of !isPostBack?

for

Void page_load()
{
if(!IsPostBack)

{
//Call GridView Binding method
}

}

Upvotes: 0

user1172910
user1172910

Reputation: 73

now here is the code for updating that session variable which holds the datatable,this must be done in RowUpdating Event of the Gridview.

protected void grdRepayment_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

        DataTable myDatatable;
        GridViewRow row = grdRepayment.Rows[e.RowIndex]; 
        grdRepayment.EditIndex = -1; 

        if (row != null)
        {
            myDatatable = (DataTable)Session["TempTable"];
            for (int i = 0; i < myDatatable.Rows.Count; i++)
            {
                if (e.RowIndex == i)
                {

                    myDatatable.Rows[i][1] = Convert.ToString(Client);
                    myDatatable.Rows[i][2] = Convert.ToString(Principal);

                    Session["TempTable"] = myDatatable;
                    grdRepayment.EditIndex = -1;
                    grdRepayment.DataSource = myDatatable;
                    grdRepayment.DataBind();

                }
            }
        }
      }

and make sure you bind the grid from this session variable only in RowEditing and RowCancellingEdit Events. Once you are done with the Editing thing simply click the button which will push the edited content to the DATABASE...

Upvotes: 0

user1172910
user1172910

Reputation: 73

morning bryan...here is that code for getting the value from the gridview to the datatable

private void getGridInfo()

{

    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("Select", typeof(byte)));
    dt.Columns.Add(new System.Data.DataColumn("Client", typeof(string)));
    dt.Columns.Add(new System.Data.DataColumn("PrincipleAmt", typeof(double)));



    foreach (GridViewRow row in grdRepayment.Rows)
    {
        CheckBox Select = (CheckBox)row.FindControl("ChkSelect");
        Label ClientName = (Label)row.FindControl("lblClientName");
        Label Principal = (Label)row.FindControl("lblPricipal");

        dr = dt.NewRow();
        dr[0] = Convert.ToByte(Select.Checked);
        dr[1] = ClientName.Text;
        dr[2] = Convert.ToDouble(Principal.Text);

        dt.Rows.Add(dr);
    }
    Session["TempTable"] = dt;
}

Upvotes: 0

The problem with your code is,you r binding grid on page load.. so when page is loading , it can bind the values which are in database. for that write bind method on Post back and also maintain your values on sessions or Hidden Fields,when you click on save button update database with that sessions or hidden fields..

Upvotes: 0

Nishanth Nair
Nishanth Nair

Reputation: 2965

Are you checking !IsPostBack inside Page_Load event before binding the grid? If you are not binding the grid in Page_Load event, please post the code to bind the grid.

Upvotes: 0

Brian Webster
Brian Webster

Reputation: 30855

You need to maintain your GridView's datasource between postbacks.

You can do this by storing your DataTable in Cache, Session, or any persistent storage.

Upon edit of a row, save the changes to this DataTable, then rebind the Gridview (from this DataTable).

When the user clicks "save all", you can save the changed rows of the DataTable to the database.

If you need to keep track of which rows have changed, you can maintain a list of PrimaryKeys that have changed in cache or Session.

Upvotes: 2

Related Questions