In this Example, we are using the pager template for paging.
DataKeyNames – Used for Getting Row in Row command.
Set the AllowPaging is to True and Set the PageSize is to your Page size count.
Eval – Used for Direct column Binding .
Within that script tag Container.DataItemIndex is used for Serial No.
Images for Paging
First.jpg
Last.jpg
Next.jpg
Previous.jpg
In Aspx Page,
<asp:GridView ID="gvEdit" runat="server" AutoGenerateColumns="False" DataKeyNames="LOBID" OnPageIndexChanging="gvEdit_PageIndexChanging" Width="100%" AllowPaging="True" PageSize="10" OnRowCommand="gvEdit_RowCommand" OnRowCreated="gvEdit_RowCreated" OnRowEditing="gvEdit_RowEditing">
<HeaderStyle HorizontalAlign="Center" BackColor="#2D96CE" ForeColor="White"/>
<AlternatingRowStyle BackColor="#D4EFFD" />
<Columns>
<asp:TemplateField HeaderText="S.No.">
<ItemTemplate>
<asp:LinkButton Style="color:Maroon;font-size:small" ID="lnkSno" runat="Server" Text='<%# Container.DataItemIndex + 1 %>' CommandName="Edit">
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOBID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblLOBID" runat="Server" Text='<%# Eval("LOBID") %>'></asp:Label></ItemTemplate>
<ItemStyle HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOB Name">
<ItemTemplate>
<asp:Label ID="lblLOBName" runat="Server" Text='<%# Eval("LOB") %>'></asp:Label></ItemTemplate>
<ItemStyle HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer">
<ItemTemplate>
<asp:Label ID="lblCustomer" runat="Server" Text='<%# Eval("Customer") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="left" />
</asp:TemplateField>
</Columns>
<PagerStyle Height="8px" HorizontalAlign="Center" />
<PagerTemplate>
<table style="width: 100%;" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" style="width: 60%;">
<table>
<tr>
<td>
<asp:ImageButton ToolTip="First Page" CommandName="Page" CommandArgument="First" runat="server" ID="ImgeBtnFirst" ImageUrl="../Images/First.jpg" />
</td>
<td>
<asp:ImageButton ToolTip="Previous Page" CommandName="Page" CommandArgument="Prev" runat="server" ID="ImgbtnPrevious" ImageUrl="../Images/Previous.jpg" />
</td>
<td>
<asp:DropDownList ToolTip="Goto Page" ID="ddlPageSelector" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlPageSelector_SelectedIndexChanged" CssClass="combo_common_nowidth">
</asp:DropDownList>
</td>
<td>
<asp:ImageButton ToolTip="Next Page" CommandName="Page" CommandArgument="Next" runat="server" ID="ImgbtnNext" ImageUrl="../Images/Next.jpg" />
</td>
<td>
<asp:ImageButton ToolTip="Last Page" CommandName="Page" CommandArgument="Last" runat="server" ID="ImgbtnLast" ImageUrl="../Images/Last.jpg" />
</td>
</tr>
</table>
</td>
<td style="width: 40%" align="right">
<asp:Label ID="lblpageindx" CssClass="labelBold" Text="Page : " runat="server"></asp:Label>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
In Code Behind(.cs file) contains the following codes,
Here objLoad is a BusinessLayer Object.
//GridView Binding Call
/// <summary>
/// Bind the Customer LOB Mapping
/// </summary>
private void BindEdit()
{
objLoad.LOBID = Convert.ToInt32(ddlSearchLOB.SelectedValue);
objLoad.Customer = ddlSearchCustomer.SelectedValue;
DataSet objSet = new DataSet();
objSet = objLoad.GetCustomerMapping();
gvEdit.DataSource = (objSet.Tables.Count != 0 ? objSet.Tables[0] : null);
gvEdit.DataBind();
if (gvEdit.Rows.Count == 0)
{
lblMsg.Text = "Oops! No Records Found.";
lblMsg.Visible = true;
}
}
//Set the Pager Row here.
protected void gvEdit_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
SetPagerButtonStates(gvEdit, e.Row, this, "ddlPageSelector");
}
}
//Get the Index no. from Page DropdownList and Bind the current page.
protected void ddlPageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
gvEdit.PageIndex = ((DropDownList)sender).SelectedIndex;
BindEdit();
}
//GridView Page Index Changing and Bind the current Page index
protected void gvEdit_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvEdit.PageIndex = e.NewPageIndex;
BindEdit();
}
//RowCommand Edit for Updation, Hyperlink button click event.
protected void gvEdit_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "Edit")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
Label lblLOBID = row.Cells[1].FindControl("lblLOBID") as Label;
Label lblCustomer = row.Cells[3].FindControl("lblCustomer") as Label;
hidEdit.Value = lblLOBID.Text + "~" + lblCustomer.Text;
ddlLOB.SelectedIndex = -1;
ddlCustomer.SelectedIndex = -1;
ddlLOB.SelectedValue = lblLOBID.Text;
ddlCustomer.SelectedValue = lblCustomer.Text;
}
}
catch (Exception ex)
{
}
}
//Set the Page settings and add the page numbers for Paging
/// <summary>
/// Setting the Pager
/// </summary>
/// <param name="gridView">Gridview Control ID</param>
/// <param name="gvPagerRow">GridView Row</param>
/// <param name="page">Page</param>
/// <param name="DDlPager">Drop down list ID</param>
public void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page, string DDlPager)
{
// to Get No of pages and Page Navigation
int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;
ImageButton btnFirst = (ImageButton)gvPagerRow.FindControl("ImgeBtnFirst");
ImageButton btnPrevious = (ImageButton)gvPagerRow.FindControl("ImgbtnPrevious");
ImageButton btnNext = (ImageButton)gvPagerRow.FindControl("ImgbtnNext");
ImageButton btnLast = (ImageButton)gvPagerRow.FindControl("ImgbtnLast");
btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount - 1));
DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl(DDlPager);
ddlPageSelector.Items.Clear();
for (int i = 1; i <= gridView.PageCount; i++)
{
ddlPageSelector.Items.Add(i.ToString());
}
ddlPageSelector.SelectedIndex = pageIndex;
string strPgeIndx = Convert.ToString(gridView.PageIndex + 1) + " of "
+ gridView.PageCount.ToString();
Label lblpageindx = (Label)gvPagerRow.FindControl("lblpageindx");
lblpageindx.Text += strPgeIndx;
}
The output looks like below,
01:05 |
Category:
ASP.NET
|
1 comments
Comments (1)
Paging GridView in C# code