Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am fetching data from sql database and displaying that data in gridview. I have some 16 records. And when I click on a button, I want to display them in gridview in 4 pages with 4 records in each page. I am beginner in vb.net and also in Asp. Please help me to solve this

My coding in vb.net is
C#
If IsPostBack Then
           Dim sql As String
           Dim adapter As New SqlDataAdapter
           Dim ds As New DataSet
           Dim Command As SqlCommand = New SqlCommand
           'Dim Command1 As SqlCommand = New SqlCommand
           Dim connection As SqlConnection = New SqlConnection("Server=pc5426;uid=so;pwd=intel;database=IDB")

SQL
sql = "select * from tblquestionbank2"


C#
Try

                connection.Open()
                Command = New SqlCommand(sql, connection)

                adapter.SelectCommand = Command
                adapter.Fill(ds)
                'GridView1.DataSource = Command.ExecuteReader
                GridView1.DataSource = ds.Tables(0)
                GridView1.DataBind()
                
                connection.Close()
            Catch ex As Exception
 End Try
End if

    End Sub

And
C#
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

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

    End Sub

My source code is:
C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" 
                ShowHeader="True" Width="529px" style="margin-top: 110px" PageSize="3" 
                AllowPaging="True" BorderColor="White" BorderStyle="Ridge" CellSpacing="1" CellPadding="3">


When I tried this code I can see pages numbered 1 2 3 4 at the bottom of gridview. But I can see only content in 1st page and if I click on 2 or 3 or 4. The gridview itself disappears.

Thanks in Advance...
Posted
Updated 22-Nov-13 1:10am
v2
Comments
Jignesh Khant 22-Nov-13 7:21am    
Call the function that populate's your gridview in PageIndexChanging.

See this example and implement as suggested there : Implement Pagination in ASP.Net Gridview Control[^]
 
Share this answer
 
add this line
viewState("data") = ds.Tables(0);
before
GridView1.DataSource = ds.Tables(0)

and modify as below

VB
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
      GridView1.DataSource = viewState("data")
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()

    End Sub
 
Share this answer
 
Comments
Member 10419145 22-Nov-13 8:10am    
I tried yours and got the desired result.
If possible can you please tell me what "viewState("data") = ds.Tables(0)" is performing.

Because I try using "GridView1.DataSource = ds.Tables(0)" directly in the "GridView1_PageIndexChanging" without using "viewState("data") = ds.Tables(0)" in "button click"
Karthik_Mahalingam 22-Nov-13 8:39am    
ViewState is the state management technique used in asp.net to retain the values even after post back..
once the data is loaded from database. it has to be stored in view state ( similar to temp data )
after that, wherever u need the data u need not to call the database again and agian.. instead u can use the viewstate....
for more info on view state refer http://msdn.microsoft.com/en-us/library/ms972976.aspx
Bind Your Grid at GridView1_PageIndexChanging
 
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