Showing posts with label Paging in gridview. Show all posts
Showing posts with label Paging in gridview. Show all posts

Thursday, July 16, 2009

Custom paging in gridview

This is a sample of customized Paging the concept implements the logic used in Google for paging the page numbers increases and decreases as in Google.. I have seen that logic and tires to implement it some how i succeeded and now i am sharing it with you all..

in this i have used a menu bar for the customized paging in the pager of a grid view....

the piece of code for the .cs file is as follows and for .aspx files and .cs files please go through the attachments..


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

binding();
}
}
private void binding()
{
DataTable tblData = new DataTable();
DataColumn col1 = new DataColumn("Data1");
tblData.Columns.Add(col1);

for (int i = 1; i <= 1500; i++)
{
DataRow dr = tblData.NewRow();
dr["Data1"] = i;
tblData.Rows.Add(dr);
}
GridView1.DataSource = tblData;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
Menu pgmenu =(Menu) e.Row.FindControl("Menu1");
int startno, endno;
int selectedpgeno = -1;
if (ViewState["selectedpgno"] == null)
{
startno = 1;
endno = startno + 9;
}
else
{
selectedpgeno = Convert.ToInt32(ViewState["selectedpgno"]);
if ((selectedpgeno - 10) <= 0)
{
startno = 1;
endno = selectedpgeno + 9;
}
else
{
startno = selectedpgeno - 10;
endno = selectedpgeno + 9;
}
}

for (int i = startno; i <= endno; i++)
{
pgmenu.Items.Add(new MenuItem(i.ToString(), (i - 1).ToString()));
}
}
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
ViewState.Add("selectedpgno", e.Item.Text);
GridView1.PageIndex = Convert.ToInt32(e.Item.Value);
binding();
}
protected void Navigate(object sender, CommandEventArgs e)
{
if (e.CommandName == "Next")
{
ViewState.Add("selectedpgno", GridView1.PageIndex + 1);
GridView1.PageIndex = Convert.ToInt32(GridView1.PageIndex + 1);
binding();
}
}
}
Download file

Thanks and Regards
Meetu Choudhary
http://www.msdotnetmentor.com/

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com