cross page postback for gridview data

Posted on 2010-01-04
Last Modified: 2012-06-21
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.
Question by:suecnus
    LVL 18

    Expert Comment


    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

    LVL 14

    Accepted Solution

    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.
    LVL 5

    Expert Comment

    The below link has complete sample soultion for the "Q" that you have asked for:

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

          Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now