ASP.Net GridView Paging using VB.Net

Updated on 03 Sep 2012,
Published on 23 Jun 2010

In ASP.Net GridView is most often used to display the data retrieved from the database in Tabular form. Another reason for using GridView control is due to its inbuilt useful features like data paging, sorting and auto formats.

You can use VB.Net code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with paging enabled. 

First of all drag the GridView control from Data controls menu.

[code:html]<asp:GridView id="GridView1" runat="server"></asp:GridView>[/code]

It will add the GridView control's HTML source code as given above. Now click on GridView control to load the control properties at right side panel.

To enable the paging in GridView control select True from the dropdown list of AllowPaging property of GridView control as shown in the above image.

It will add AllowPaging="True" in HTML source code of Gridview.

Next step is to bind the data with Gridview control and handle the GridView page event.

To bind the PageIndexChanging event of GridView control, double click on the PageIndexChanging event in the properties of Gridview. It will add the event in HTML source code as well as VB.Net code.

HTML source code for GridView Control

<asp:GridView id="GridView1" 
    runat="server" 
    AllowPaging="True" 
    OnPageIndexChanging="GridView1_PageIndexChanging" 
    PageSize="5">
</asp:GridView>

You can set the PageSize value as per your requirement.

VB.Net Code for GridView Paging in ASP.Net

Import the following namespaces to work with SQL database:

[code:vb]Imports System.Data
Imports System.Data.SqlClient[/code]

The above namespaces will allow you to access the SqlConnection, SqlCommand, SqlAdapter classes which provide the functionality to connect the SQL database and fetch the records from the specified table as in the following VB.Net code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        bindGridView()
    End If
End Sub

Public Sub bindGridView()
    ' string variable to store the connection string
    ' defined in appsettings section of web.config file.
    Dim connStr As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString

    ' object created for SqlConnection Class.
    Dim mySQLconnection As New SqlConnection(connStr)

    ' if condition that can be used to check the sql connection
    ' whether it is already open or not.
    If mySQLconnection.State = ConnectionState.Closed Then mySQLconnection.Open()

    Dim mySqlCommand As New SqlCommand("select * from products", mySQLconnection)
    Dim mySqlAdapter As New SqlDataAdapter(mySqlCommand)
    Dim myDataSet As New DataSet()
    mySqlAdapter.Fill(myDataSet)

    GridView1.DataSource = myDataSet
    GridView1.DataBind()

    ' if condition that can be used to check the sql connection
    ' if it is open then close it.
    If mySQLconnection.State = ConnectionState.Open Then mySQLconnection.Close()
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    bindGridView()
End Sub

Output:

Get the output for above discussed code from the following link:

VB.Net GridView Paging

After data binding with GridView control you will get the results as shown in the above image of Gridview control on page load.

Continue to the next tutorial: ASP.Net Gridview Sorting using VB.Net to learn how to add the sorting functionality to the gridview control.

3 Responses to "ASP.Net GridView Paging using VB.Net"
Mannuel
Hello, one question, i need the code to paging and sorting in same gridview in vb.net please.
mario
works fine.. but what if I want to use a different command string than the first one later in my program?
Abhijit
Thanks...You saved my life...:)
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers