• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

cross page postback for gridview data

I am trying to use cross page postback to pass data of one grid to another page. I could get the instance of the grid in the target page and it is populated as it the rowcount returns the same number of rows as the gridview from the source page.

                  Dim gridview2 As New GridView
                    gridview2 = CType(PreviousPage.FindControl("GridView1"), GridView)

My question is, how can I bind the datasource to the gridview in the target page? I used this

                        Dim dt As DataTable = CType(gridview2.DataSource, DataTable)
but the datatable returns null.
1 Solution

Did you try:

Dim dt As DataTable = CType(CType(PreviousPage.FindControl("GridView1"), GridView).DataSource, DataTable)

Otherwise I would suggest, depending on the number of records, to load the datasource into a session object, that way you can easily retrieve it on the other page, i.e:

On page 1 (dt being the datasource table):

Session("gv1DataSource") = dt

On page 2:

If Not Session("gv1DataSource") Is Nothing Then
    Dim dt As DataTable = CType(Session("gv1DataSource"), DataTable)
    GridView2.DataSource = dt
End If

It should work, if the DataSource for the first GridView1 is a DataTable. If it is a DataView, then you have to declare a DataView and cast the Datasource to a DataView. Works the same way as you did with the datatable.
The below link has complete sample soultion for the "Q" that you have asked for:

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now