Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

custom paging of the datagrid or gridview

Posted by Viral Sarvaiya on August 20, 2009

hear i demonstrate the custom paging of the datagrid or gridview.

in the client side take the datagrid or grid view

here i take the datagrid,


<table>

<tr>
<td>

<asp:datagrid id="dgMake" runat="server" AutoGenerateColumns="False" Width="100%"
CellPadding="5" GridLines="None" AllowPaging="true">

<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Left" Width="100"></HeaderStyle>
<HeaderTemplate>Part</HeaderTemplate>
<ItemStyle HorizontalAlign="Left" Width="100"></ItemStyle>
<ItemTemplate><%#Container.DataItem("part")%></ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<HeaderTemplate>Description</HeaderTemplate>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate><%#Container.DataItem("description")%></ItemTemplate>
</asp:TemplateColumn>

</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid>

</td>
</tr>

in the second raw, i add the link buttons as below,


<tr>
<td valign="top" align="right" bgcolor="#e1e4e6" style="padding:10px 10px 10px 60px;text-align:right;">
<asp:linkbutton id="FirstButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="0"> First Page</asp:linkbutton><asp:label id="lblFirstLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="PrevButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="prev">Prev</asp:linkbutton><asp:label id="lblLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="NextButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="next">Next</asp:linkbutton><asp:label id="lblLastLine" runat="server">&nbsp;|&nbsp;</asp:label>
<asp:linkbutton id="LastButton" onclick="PagerButtonClick" runat="server" CssClass="contant" CommandArgument="last"> Last Page</asp:linkbutton>
</td>
</tr>

in the code-behind file, bind the datagrid as below,

define the page size of the datagrid in the page load


dgMake.PageSize = 50

then call this function in the postback false part of the page load.


sub bind_datagrid()

Dim ds As New DataSet

ds = classobject.fiunctionname()

If ds.Tables(0).Rows.Count > 0 Then

dgMake.DataSource = ds

dgMake.DataBind()

firstprev()

Else

dgMake.DataSource = ds
dgMake.DataBind()

FirstButton.Visible = False
PrevButton.Visible = False
NextButton.Visible = False
LastButton.Visible = False
lblFirstLine.Visible = False
lblLastLine.Visible = False
lblLine.Visible = False

End If
End Sub

Public Sub firstprev()
If dgMake.CurrentPageIndex <> 0 Then
Call Prev_Buttons()
FirstButton.Visible = True
PrevButton.Visible = True
lblFirstLine.Visible = True
Else
FirstButton.Visible = False
PrevButton.Visible = False
lblFirstLine.Visible = False
End If

'To Display Next/Last Page buttons

If dgMake.CurrentPageIndex <> (dgMake.PageCount - 1) Then
Call Next_Buttons()
NextButton.Visible = True
LastButton.Visible = True
lblLastLine.Visible = True
Else
NextButton.Visible = False
LastButton.Visible = False
lblLastLine.Visible = False
End If

If dgMake.CurrentPageIndex > 0 And dgMake.CurrentPageIndex < dgMake.PageCount - 1 Then
lblLine.Visible = True
Else
lblLine.Visible = False
End If

End Sub

Sub Prev_Buttons()
Dim PrevSet As String
If dgMake.CurrentPageIndex + 1 <> 1 Then
PrevSet = dgMake.PageSize
PrevButton.Text = ("Prev")
End If
End Sub

Sub Next_Buttons()
Dim NextSet As String
If dgMake.CurrentPageIndex + 1 < dgMake.PageCount Then
NextSet = dgMake.PageSize
NextButton.Text = ("Next")
End If
End Sub

Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim arg As String = sender.CommandArgument
Select Case arg
Case "next"
'The next Button was Clicked
If (dgMake.CurrentPageIndex < (dgMake.PageCount - 1)) Then
dgMake.CurrentPageIndex += 1
End If

Case "prev"
'The prev button was clicked
If (dgMake.CurrentPageIndex > 0) Then
dgMake.CurrentPageIndex -= 1
End If

Case "last"
'The Last Page button was clicked
dgMake.CurrentPageIndex = (dgMake.PageCount - 1)

Case Else
'The First Page button was clicked
dgMake.CurrentPageIndex = Convert.ToInt32(arg)
End Select
'selpagecombo()
'Now, bind the data!
bind_datagrid()
End Sub

so the page will be bind to 50 records.

enjoy coding…..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: