Sample code for ASP.NET DataGrid paging


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Sample code for ASP.NET DataGrid paging

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Bangalore, India
    Posts
    47

    Sample code for ASP.NET DataGrid paging

    Hi,

    Here is a sample code for ASP.NET DataGrid paging


    The DataGrid control has built-in support for paging through the records of a data source.
    For example, there may be a large number of records in the AccountsTable. If we divide the records
    into multiple pages, it is convenient for the user to see the data.
    We enable paging for a DataGrid by enabling the AllowPaging property and
    creating a subroutine to change the current page.

    Write the following code in the HTML window

    <HTML>
    <HEAD>
    <title>DatagridPaging</title>
    </HEAD>
    <body>
    <form id=”Form1" runat=”server”>

    <asp:DataGrid ID=”dgrdAccounts” runat=”server”
    AllowPaging=”True” PageSize=”5"
    OnPageIndexChanged=”drdaccounts_pageIndexChanged”
    cellpadding=”3">
    </asp:DataGrid>
    </form>
    </body>
    </HTML>

    Write the following code in the Code behind window

    Imports System.Data.SqlClient

    Dim myConnection As SqlConnection = New SqlConnection(“Data Source=SYS1;Integrated Security=SSPI;Initial Catalog=FinAccounting”)
    Const strSQL As String = “SELECT AccountCode,Accountname,AccountDescription FROM AccountsTable”
    Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
    Dim dstaccounts As New DataSet()

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack() Then
    BindDataGrid()
    End If
    End Sub

    Sub BindDataGrid()
    myConnection.Open()
    myDataAdapter.Fill(dstaccounts, “AccountsTable”)
    dgrdAccounts.DataSource = dstaccounts
    dgrdAccounts.DataBind()
    End Sub

    Sub drdaccounts_pageIndexChanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs)
    dgrdAccounts.CurrentPageIndex = e.NewPageIndex
    BindDataGrid()
    End Sub

    This example displays five records at a time from the AccountsTable. By clicking the
    < and > links displayed at the bottom of the DataGrid,
    we can navigate forward or backward. A new page is selected
    with the drdaccounts_pageIndexChanged subroutine.
    This subroutine assigns the value of the NewPageIndex to the DataGrid control’s CurrentPageIndex.
    The subroutine then rebinds the DataGrid to the data source, displaying the new page of records.

    Regards
    bhar
    Last edited by Phil Weber; 11-25-2005 at 12:07 AM. Reason: Disabled smilies, deleted advertising

Similar Threads

  1. About these DataGrid Paging thingies!
    By Lars Netzel in forum .NET
    Replies: 2
    Last Post: 06-10-2002, 07:04 AM
  2. Connect a DataGrid through code
    By Philip L in forum VB Classic
    Replies: 7
    Last Post: 05-06-2001, 08:34 PM
  3. Replies: 0
    Last Post: 04-03-2001, 04:32 PM
  4. Replies: 0
    Last Post: 01-22-2001, 05:29 PM
  5. Sample Code
    By Bill Putnam in forum XML
    Replies: 1
    Last Post: 03-24-2000, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center