Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Morning guys,

My Gridview page only works from 1st page to 2nd and to 3rd...
But if I am on 3rd page and click on 2nd or 1st it doesn't do anything, instead just sits on page 3 itself.
Here is my code
SQL
<asp:GridView ID="gdvAll" runat="server" AllowPaging="true" PageSize="10" AllowSorting="true"
                               Style="vertical-align: middle;" HorizontalAlign="Center" OnPageIndexChanging ="ongdvPageIndex_Clicked"
                               AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" EnableSortingAndPagingCallbacks="true"
                               CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr">

Protected Sub ongdvPageIndex_Clicked(ByVal Sender As Object, ByVal e As GridViewPageEventArgs)
    Try
        gdvAll.PageIndex = e.NewPageIndex
        gdvAll.DataSource = ViewState("gdvAll")
        gdvAll.DataBind()
    Catch ex As Exception
        LogError.LogErrorIntoTextFile(ex, "ongdvPage_Clicked()")
    End Try
End Sub


Can anyone figure out this weird behavior of pages in Gridview?
Posted
Comments
sudevsu 25-Feb-15 9:29am    
I have two update panels in same page. Do you think that is the cause of this weird behavior?

1 solution

I believe your ViewState("gdvAll") is a dataset from another Subroutine with some sort of SQL or other. This code will sort the data by Ascending or Descending alleviating you Page Problem:
VB
Protected Sub gdvAll_OnSorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting
      Dim sortingdirection, sortby
      If e.SortDirection = "ASC" Then
          sortingdirection = "DESC"
      Else
          sortingdirection = "ASC"
      End If
      sortby = e.SortExpression & " " & sortingdirection
      Dim MyDataSet As New DataSet = ViewState("gdvAll")
      Dim MyDataTable As New DataTable()
      MyDataTable = MyDataSet.Tables(0)
      Dim MyDataView As New DataView(MyDataTable)
      MyDataView.Sort = sortby
      gdvAll.DataSource = ViewState("gdvAll")
      GridView1.DataBind()
  End Sub

SQL
<asp:GridView ID="gdvAll" runat="server" AllowPaging="true" OnPageSize="10" AllowSorting="true"  OnSorting="gdvAll1_OnSorting"
                               Style="vertical-align: middle;" HorizontalAlign="Center" OnPageIndexChanging ="ongdvPageIndex_Clicked"
                               AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" EnableSortingAndPagingCallbacks="true"
                               CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr">
 
Share this answer
 
v3

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