ASP.Net GridView Paging using VB.Net
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.
GridView Control Examples:
You can see the live samples and examples of GridView Control from the following links:
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
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.
