Custom Pagination in GridView with Formatted using Asp.net

Custom Pagination in GridView with Formatted using Asp.net

Follow the Below Code to achieve result
Default.aspx————————————————————————————————

<asp:GridView ID=”CustomGrid” runat=”server” EnableModelValidation=”True” AutoGenerateColumns=”False”
AllowPaging=”True” BackColor=”White” BorderColor=”#DEDFDE” BorderStyle=”None”
BorderWidth=”1px” CellPadding=”4″ ForeColor=”Black” GridLines=”Vertical”
EmptyDataText=”No record found.” PageSize=”2″>
<AlternatingRowStyle BackColor=”White” />
<Columns>

<asp:TemplateField HeaderText=”UserID”>
<ItemTemplate>
<asp:Label ID=”lblUser” runat=”server” Text='<%# Eval(“ID”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”NAME” HeaderText=”Name”>
</asp:BoundField>
<asp:BoundField DataField=”PRICE” HeaderText=”Price”>
</asp:BoundField>
</Columns>
<FooterStyle BackColor=”#CCCC99″ />
<HeaderStyle BackColor=”#6B696B” Font-Bold=”True” ForeColor=”White” />
<PagerSettings FirstPageText=”First” LastPageText=”Last” NextPageText=”Next” Position=”TopAndBottom”
PreviousPageText=”Prev” />
<PagerStyle BackColor=”#F7F7DE” ForeColor=”Black” HorizontalAlign=”Right” />
<PagerTemplate>
<div>
<span style=”float: right;”>
<asp:ImageButton ID=”ImageButtonNext” runat=”server” ImageUrl=”~/images/r-arrow.png”
OnClick=”ImageButtonNext_Click” />
</span><span style=”float: right; padding-left: 5px; padding-right: 5px;”>
<asp:Label ID=”LabelCurrentIndex” runat=”server” Text='<%= CurrentSelectedIndex %>’></asp:Label>
Of
<asp:Label ID=”LabelLastIndex” runat=”server” Text='<%= TotalPageCount %>’></asp:Label>
</span><span style=”float: right;”>
<asp:ImageButton ID=”ImageButtonPrev” runat=”server” ImageUrl=”~/images/l-arrow.png”
OnClick=”ImageButtonPrev_Click” />
</span>
</div>
</PagerTemplate>
<RowStyle BackColor=”#F7F7DE” />
<SelectedRowStyle BackColor=”#CE5D5A” Font-Bold=”True” ForeColor=”White” />
</asp:GridView>
<input type=”hidden” id=”GridActiveIndex” value=”0″ runat=”server” />
Default.aspx.cs————————————————————————————————

public string currentSelectedIndex = string.Empty;
public string totalPageCount = string.Empty;
public string UserEmail = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
int currentIndex = Convert.ToInt32(Convert.ToString(GridActiveIndex.Value));
if (currentIndex > 0)
{
CustomGrid.PageIndex = currentIndex;
}

}

}
private void LoadData()
{
DataTable dt = new DataTable();
dt = getResultTable();
CustomGrid.DataSource = dt;
CustomGrid.DataBind();
GridActiveIndex.Value = “0”;
FotmatPager(0);
}
DataTable getResultTable()
{
DataTable dtproduct = new DataTable();
dtproduct.Columns.Add(“ID”, typeof(int));
dtproduct.Columns.Add(“NAME”, typeof(string));
dtproduct.Columns.Add(“PRICE”, typeof(int));

// Here we add five DataRows.
dtproduct.Rows.Add(1, “Product1”, 100);
dtproduct.Rows.Add(2, “Product2”, 110);
dtproduct.Rows.Add(3, “Product3”, 120);
dtproduct.Rows.Add(4, “Product4”, 130);
dtproduct.Rows.Add(5, “Product5”, 140);

return dtproduct;
}
#region GridVariable

GridViewRow pagerRow;
int current_index;
int page_count;

#endregion
public string CurrentSelectedIndex
{
get { return currentSelectedIndex; }
set { currentSelectedIndex = value; }
}
public string TotalPageCount
{
get { return totalPageCount; }
set { totalPageCount = value; }
}
protected void formatTopPager(int current, int total)
{
try
{
pagerRow = CustomGrid.TopPagerRow;
Label lblcurrentindex =
(Label)pagerRow.Cells[0].FindControl(“LabelCurrentIndex”);
lblcurrentindex.Text = current.ToString();
Label lbllastindex =
(Label)pagerRow.Cells[0].FindControl(“LabelLastIndex”);
lbllastindex.Text = total.ToString();
CurrentSelectedIndex = current.ToString();
TotalPageCount = total.ToString();
}
catch (Exception)
{

//throw;
}

}
protected void fotmatBottomPager(int current, int total)
{
try
{
pagerRow = CustomGrid.BottomPagerRow;
Label lblcurrentindex =
(Label)pagerRow.Cells[0].FindControl(“LabelCurrentIndex”);
lblcurrentindex.Text = current.ToString();
Label lbllastindex =
(Label)pagerRow.Cells[0].FindControl(“LabelLastIndex”);
lbllastindex.Text = total.ToString();
CurrentSelectedIndex = current.ToString();
TotalPageCount = total.ToString();
}
catch (Exception)
{

// throw;
}

}
protected void FotmatPager(int current)
{
ViewState[“currentPageIndex”] = current;
current += 1;

int total = CustomGrid.PageCount;
formatTopPager(current, total);
fotmatBottomPager(current, total);
CustomGrid.PageIndex = Convert.ToInt32(Convert.ToString(ViewState[“currentPageIndex”]));

}
protected void ManageGridLayout(DataTable dt, int selected_pageIndex, int totalPage)
{

CustomGrid.PageIndex = selected_pageIndex;
CustomGrid.DataSource = getResultTable();
CustomGrid.DataBind();
FotmatPager(selected_pageIndex);

}
protected void ImageButtonNext_Click(object sender, ImageClickEventArgs e)
{
current_index = CustomGrid.PageIndex;
page_count = CustomGrid.PageCount;
if (current_index + 1 < page_count)
ManageGridLayout(getResultTable(), current_index + 1, page_count);
}
protected void ImageButtonPrev_Click(object sender, ImageClickEventArgs e)
{
current_index = CustomGrid.PageIndex;
page_count = CustomGrid.PageCount;
if (current_index > 0)
ManageGridLayout(getResultTable(), current_index – 1, page_count);

}

FormatPagination

Download Example

Custom Pagination With Formatted

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply