how to implement paging in datagrid?
how to implement paging in datagrid?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Aug 22, 2012, 5:34 AM
Paging in a Datagrid in two modes.
Program
Default.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Paging in a Datagridtitle>
<style type="text/css">
.hr1
{
color: #f00;
background-color: #f00;
height: 5px;
}
style>
head>
<body>
<form id="form1" runat="server">
<div>
<hr class="hr1" />
<asp:Label ID="lb1" Text="NextPrevious Mode" BackColor="Yellow" ForeColor="Red" runat="server">asp:Label>
<asp:DataGrid id="DataGrid1" BackColor="#FF9999" AllowPaging="True" PageSize="3" OnPageIndexChanged="datagrid1_pageindexchanged" PagerStyle-Mode="NextPrev" runat="server" Width="328px">
<HeaderStyle BackColor="#FFCC99" />
asp:DataGrid>
<hr class="hr1" />
<asp:Label ID="Label1" Text="NumericPages Mode" BackColor="Yellow" ForeColor="Red" runat="server">asp:Label>
<asp:DataGrid id="DataGrid2" BackColor="#FF9999" AllowPaging="True" PageSize="3" OnPageIndexChanged="datagrid2_pageindexchanged" PagerStyle-Mode="NumericPages" runat="server" Width="328px">
<HeaderStyle BackColor="#FFCC99" />
asp:DataGrid>
<hr class="hr1" />
div>
form>
body>
html>
Default.aspx.vb code
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim sqlda As SqlDataAdapter
Dim com As SqlCommand
Dim ds As DataSet
Dim str As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bindgrid1()
bindgrid2()
End If
End Sub
Sub bindgrid1()
Try
con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "student"
DataGrid1.DataBind()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub bindgrid2()
Try
con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet
sqlda.Fill(ds, "student")
DataGrid2.DataSource = ds
DataGrid2.DataMember = "student"
DataGrid2.DataBind()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Protected Sub datagrid1_pageindexchanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
bindgrid1()
End Sub
Protected Sub datagrid2_pageindexchanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged
DataGrid2.CurrentPageIndex = e.NewPageIndex
bindgrid2()
End Sub
End Class
Thanks
If this post helps you mark it as answer
Sukesh MarlaPosted Aug 22, 2012, 1:28 AM
Check this is correct answer if it helped