Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a GridView that shows particular data and records when I click Generate button.
My problem is that when I click for the next page of the GridView list the Gridview will be lost (blank), I don't think the data will be lost too, I guess the GirdView just doesn't show it.
Can you please help me with this problem. I'm not the one who first create this website and I tried all the things that I know for this problem, I also searched for a solutions in the internet with no luck. So I hope someone can tell me why is the GridView not showing after clicking the next page.

Here's the code for the Page_Load

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)

    V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
    V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")

    If Not Page.IsPostBack Then
        V1Division.DataSource = GetDivision()
        V1Division.DataBind()

        V1BR.Items.Clear()
        V1BR.DataSource = GetBusinessRule(V1Division.Text)
        V1BR.DataBind()

        MultiView1.ActiveViewIndex = 0
        Panel1.DefaultButton = "V1Generate"
    End If
End Sub


Here's the code for GridView1_PageIndexChanging

VB
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
    GridView1.DataSource = SortTable(dt)

    GridView1.PageIndex = e.NewPageIndex
    GridView1.DataBind()
End Sub


Here's the code for BindGridView

VB
Private Sub BindGridview(ByVal query As String, ByVal countquery As String)
    ViewState.Add("Query", query)
    ViewState.Add("CountQuery", countquery)

    Dim count As Integer = 0

    If Description.Text = "1" Then
        ValidateFamily(query)
    Else
        DataSource = dbHelper.GetRecordDT(query)
    End If

    count = DataSource.Rows.Count

    'GridView1.DataSource = Nothing
    'GridView1.DataBind()
    'GridView1.PageIndex = 0

    GridView1.DataSource = DataSource
    GridView1.Visible = True
    GridView1.DataBind()

    ExportToExcel.Visible = IIf(count < 1, False, True)
    Panel1.Visible = IIf(count < 1, False, True)
    SetTotalResult(count)
End Sub


What I have tried:

First I tried putting

VB
GridView1.Visible = True
in GridView1_PageIndexChanging and the result is the GridView with the data will show and when I click the next page it shows No Record Found.

I also tried putting this code in Page_Load

VB
If ViewState.Item("Query") <> Nothing Then
    BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
End If


The result is when I click the Generate button first the data will show in the GridView and also when I click the next page but when I choose another item in the DropDownList and click Generate button again the data will be lost again (blank).
Posted
Updated 12-Feb-17 21:57pm
v2
Comments
F-ES Sitecore 9-Feb-17 10:17am    
Do you have the gridview's viewstate enabled? If you do I don't think you need to rebind the way you are in the page changed event, and if you don't then the grid view will have no datasource when you postback as it isn't persisted. Rather than getting the data from GridView1.DataSource you need to re-bind the data from the database the way you do in the load event.you.
RKeyy Sii 12-Feb-17 21:13pm    
Sorry for the late reply, I haven't been online for the past few days.
I have the gridview's viewstate enabled as false and the gridview will be lost when I click the next page and when I enabled it as true it shows No Record Found. Can you tell me what can I do for this? Thanks.

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging   
    GridView1.PageIndex = e.NewPageIndex    
 BindGridview("query","countquery")
End Sub
 
Share this answer
 
v2
Comments
RKeyy Sii 13-Feb-17 0:45am    
@zafar Now it shows the GridView but when I click for the next page all the rows are blank.
Zafar A khan 13-Feb-17 1:00am    
in BindGridView("query","countquery") . the query and count query should have records. if there is no record in second page you can only see gridview
RKeyy Sii 13-Feb-17 1:04am    
yes it has records. when I click generate button it shows the first page with records but when I click for the next page it shows the gridview with blank records and even if I go back again to page one it remains blank. I'm sorry I ask too many question I'm new to this.
Zafar A khan 13-Feb-17 1:13am    
check the improved solution
RKeyy Sii 13-Feb-17 1:17am    
it's still the same showing the gridview with blank records
You need to data bind again inside the PageIndexChanging event, or on any event on the control for that matter.
 
Share this answer
 
Comments
RKeyy Sii 12-Feb-17 22:35pm    
But I already DataBind it inside the PageIndexChanging. What could possibly be the problem?

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
GridView1.DataSource = SortTable(dt)
GridView1.DataBind()
End Sub
Thanks for helping me out guys. I've been doing this for a while now and I did so many possible approach and solution and finally it works now.

Here's what I did.

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)

            V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
            V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")

            If Not Page.IsPostBack Then
                V1Division.DataSource = GetDivision()
                V1Division.DataBind()

                V1BR.Items.Clear()
                V1BR.DataSource = GetBusinessRule(V1Division.Text)
                V1BR.DataBind()

                MultiView1.ActiveViewIndex = 0
                Panel1.DefaultButton = "V1Generate"

                If ViewState.Item("Query") <> Nothing Then
                    BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
                End If
            End If
        End Sub


and

VB
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
            Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
            GridView1.DataSource = SortTable(dt)

            GridView1.PageIndex = e.NewPageIndex
            'GridView1.DataBind()
            If ViewState.Item("Query") <> Nothing Then
                BindGridview(ViewState.Item("Query"), ViewState.Item("CountQuery"))
            End If
        End Sub


Thanks a lot for helping me out guys :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900