C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Training   ASP.NET Web Hosting    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

Play Silverlight Games or Submit your Silverlight applications and earn 90% AdSense revenue.

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


Posted Date: 17 Nov 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: Maneesh JainMember Level: Gold    
Rating: Points: 10



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.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Paging  .  Gridview  .  Datagrid  .  Asp.net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: To get the repeater item on button click in repeater and delete row from table ion c#
Previous Resource: gridview colum color
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use