C# ASP.NET – GridView : How to Keep Modified Data of Template Fields when Paging is Enabled?

2008-12-04

Today I had to take care of a problem of a certain company (CIS) that I consult. They had a problem of keeping the values modified in a template field of a gridview when the user moves from one page of the grid to another.

Template fields allow us to add custom columns into a datagrid or gridview. For an instance, we can add a grid column which has a textbox in each cell.

When the gridview has several pages, the gridview holds only the rows that are displayed in its Rows (collection) property. So we cannot access the values in other pages using the gridview.Rows property.

Then I thought that I could access the whole collection of rows by referring to the DataSource property of the gridview. This is a good idea because even though the gridview only shows the rows on the current page for display purposes, the data source property of the gridview holds the whole collection of rows in it.

ASP.Net clears the data source of any control at post backs. This is done to optimize performance of communications. In addition, the state of each control is stored in ViewState so it is not necessary to keep the datasource between postbacks.

The problem is worse now because I could not use both datasource and the direct row collection from the gridview.

Of course we can use either cache or session to keep the datasouce.


Session["DataSource"] = dt;  // store our table in the session

Then to synchronize (update) the datasource which was stored in the session we have to:

  • Go to each row in the gridview
  • Get the matching data row from the data table
  • Update the fields of the data table row

Since we have enabled paging for the gridview, we have to do this when ever user changes the page he/she is viewing to preserve his/her modifications.

So we need to write our code in the PageIndexChanging event handler of the GridView.

In my example I have two columns of the grid view called “ID” and “Name” and the name is the only template column I have used so I am only updating that column in the stored data table.

Code:


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// get the datatable from the
DataTable dTable = (DataTable)Session["dataSource"];

// now we will iterate through all rows of the grid
// then get the matching row from the data table (datasource of our grid)
// and append the updated data (by the user) to the selected data row
foreach (GridViewRow grv in this.GridView1.Rows)
{
// get the matching row from the data table
DataRow dRow = dTable.Rows.Find(this.GridView1.DataKeys[grv.RowIndex].Value);

// set values of updated columns : here I have only let the user to edit "Name" column
dRow["Name"] = ((TextBox)grv.FindControl("txtName")).Text;
}

// go to the next page of the grid
this.GridView1.PageIndex = e.NewPageIndex;
this.GridView1.DataSource = (DataTable)Session["DataSource"];
this.GridView1.DataBind();

// show the whole collection of data in the second grid (used only to display)
this.GridView2.DataSource = (DataTable)Session["DataSource"];
this.GridView2.DataBind();
}

I have used a different gridview called GridView2 to show the whole data source without paging.

So don’t misunderstand the use of GridView2 in the last section of the code. It’s used only do display the whole set of data (without paging).

I cannot upload my code into this blog because it seems like zip files are not supported. If you like to take a glance of my code just add a comment so I’ll send the code to you via email.

Was this post helpful to you? How can I improve? – Your comment is highly appreciated!

Cassian Menol Razeek

16 Comments

  1. Deep says:

    Hi,

    I would like to go over you source code if possible. Could you please send i across?

    Thanks,

    Deep Kumar

  2. admin says:

    Hi Kumar,

    I sent the source code to the email that you had provided.

    Glad to help you!

    Menol

  3. Bhanu says:

    Hi,
    Could you please send the source code of this. I need the gridview with checkboxes and textbox fields accross all the pages, I maintained their state when page index is changing in a session value , mean while am even binding those values in the Datatable when page index was changed.
    But the problem here when i submit the page, only 1st page values are saved into the datatable.
    Could u please send the source code of this so that i can check where did i go wrong in my code.

    Thanks,
    Bhanu Ananthoju

  4. Menol says:

    Hi Bhanu,

    I sent the source code to the email that you had provided.

    Glad to help you!

    Menol

  5. Bhanu says:

    Hi Menol,

    Thanks for providing the source but my requirement is with checkboxes and textbox fields along with paging. The code is just maintaing the modified fields in session of datatable. My Grid has two columns of checkboxes, image and a textboxes, Am maintaining the states of all the checkboxes and textboxes value too while paging in an arraylist for checkboxes and hashtable for textbox values. But the problem here is I need save all fields values accross pages which are selected and value entered in the textbox.

    Thanks,
    Bhanu Ananthoju

  6. Opima Jude says:

    I like the article, its grate. i would love to check out the source code. thanks

  7. Anna says:

    do you know how to display a empty gridview?

  8. teena namlas says:

    Please send me source code.

  9. hoteitubetjg says:

    [url=http://community.bsu.edu/members/Accutane+Online.aspx]buy Accutane free shipping Volume [/url] [url=http://strox.viviti.com/]tramadol and canine and side effects organisations [/url] [url=http://bkfuller.com/members/buy-Zolpidem-online-cod-no-prescription.aspx]cheap order prescription Zolpidem ROLAND [/url] [url=http://www.rhrealitycheck.org/user/ambien-online]ambien free fedex shipping J316Savioraol [/url] [url=http://mipagina.univision.com/tramadol4t52]tramadol hcl chemical supplier white soluble event [/url] [url=http://mipagina.univision.com/Soma1Online/]cheap soma overnight sendiri [/url] [url=http://my.pantagraph.com/buyxanaxnoprescription/blog]xanax rx lebar [/url] [url=http://community.bsu.edu/members/Zithromax+Online.aspx]zithromax no doctor Allard [/url] [url=http://my.pantagraph.com/TramadolNOPRESCRIPTION/blog]effects medication side tramadol phenomenal [/url] [url=http://strox.viviti.com/tramadol-without-prescription]cash delivery shipped tramadol Vanderver [/url] [url=http://www.rhrealitycheck.org/user/xanax-no-prescription]buy xanax without Pecina [/url] [url=http://strox.viviti.com/tramadol-overnight]buy tramadol with cod conference [/url] [url=http://geoserver.org/display/~valtrex4fc7]purchase Valtrex cod shipping ponsbrooks [/url] [url=http://www.codeplex.com/site/users/view/ultram_online_4y]ultram pain management Wilsonlackland [/url] [url=http://geoserver.org/display/~lexapro4bv2]buy Lexapro free fedex shipping Liberty [/url] [url=http://mipagina.univision.com/phentermine3e4/]how to buy phetermine on line guessed [/url] [url=http://riderx.info/members/buy-Diazepam-online-cod-no-prescription.aspx]where can i buy diazepam online 4939899 [/url] [url=http://www.rhrealitycheck.org/user/viagra-no-prescription]buy viagra online consultation exceed [/url] [url=http://geoserver.org/display/~ativan4bv6]cheap ativan cod free fedex PerlbinPerl [/url] [url=http://my.pantagraph.com/somawatsononline/blog]cheap soma fedex customizable [/url]

  10. Naveen. says:

    Hi menol,
    hope u r ok and doing well.
    i need ur favour,that im struggling to keeping the 1st page template field values..while paging. could u plz forward the coding samples to me..

    Thank u
    Naveen.

  11. kemi says:

    Is is possible to have your source code? I’ve been searching for a solution to retaining values of textboxes in a gridview control.

    Thanks,
    Kemi

  12. Vikas Goyal says:

    Hi,

    I am having exactly the same problem as mentioned above(“How to Keep Modified Data of Template Fields when Paging is Enabled?”).Can you please provide me with the source code for this problem.
    Any help would be appriciated.
    Thanks in advance

    Regards
    Vikas Goyal

  13. Siva says:

    Please send me the code. I would like to go through it.

    Regards
    Siva

  14. Saravanan says:

    Hi Menol,

    I would like to view the Source Code, Kindly Send it.

    Thanks for your Help
    Saravanan

  15. Now all became clear, many thanks for the information. You have very much helped me.

  16. Shahrouz Ebadian says:

    Hi I would love to see the code for this as well.

    thanks!

Leave a Reply