Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
|
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Resources » Code Snippets » ASP.NET GridView »
Implement custom paging in datagrid
|
The ASP.NET Datagrid control is as simple as drag and drop, set some of the properties like DataSource and bind the datagrid to any of the objects like Dataset / Datareader. Having the Support for paging and sorting we only need to add a few lines of code and we can go ahead... In this article we'll try to achieve
a) Custom Paging with "First", "Previous", "Next", "Last" buttons. b) Tie up the "Page Up" and "Page Down" keys to the Datagrid. a) Custom Paging with "First", "Previous", "Next", "Last" buttons. Lets initially begin with the Datagrid Control We'll set the AllowPaging Property to True. We set the PagerStyle-Visible as false as we have chosen to do custom paging. For this sample code we have kept the default page size i.e. 10 PagerStyle-Visible="False" HeaderStyle-BackColor="Blue" HeaderStyle-ForeColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal">
The simple task to be done is to display the data in the datagrid The coding steps involve following steps : Step 1:
Have a subroutine to Bind data to Datagrid. So in the subroutine BindData() we'll have following steps done: a) Connecting to Database. b) Creating DataAdapter to execute the SQL query using the proper connection. c) Fill the Dataset Based on the DataAdapter d) Assign the DataSource Property of DataGrid to the DataSet Dim MyConnection As SqlConnection Dim myda As SqlDataAdapter Dim ds As DataSet Dim sqlStr As String = "SELECT Productid,ProductName,UnitPrice from Products" Dim strConn As String = "server=localhost;uid=sa;pwd=;database=northwind;" Dim TotalRows As Integer MyConnection = New SqlConnection(strConn) myda = New SqlDataAdapter(sqlStr, MyConnection) ds = New DataSet() myda.Fill(ds, "Datagrid1") DataGrid1.DataSource = ds DataGrid1.DataBind()
Step 2:
Now moving on to write the code for the 4 buttons "First", "Previous", "Next", "Last"
Depending upon which button is clicked we'll handle the events. The Buttons will call a server side code i.e PagerButtonClick. Based on the CommandArgument we'll manipulate with the CurrentPageIndex of the Datagrid.
The code goes as below: Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs) Dim arg As String = sender.CommandArgument Select Case arg Case "Next" If (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1)) Then DataGrid1.CurrentPageIndex += 1 End If Case "Prev" If (DataGrid1.CurrentPageIndex > 0) Then DataGrid1.CurrentPageIndex -= 1 End If Case "Last" DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1) Case Else DataGrid1.CurrentPageIndex = Convert.ToInt32(arg) End Select BindData() End Sub
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|