0

How do I add paging to this GridView?

C# code:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}

Error

Specified argument was out of the range of valid values. Parameter name: index

Rahul
  • 5,603
  • 6
  • 34
  • 57
user3168680
  • 35
  • 1
  • 8
  • asp.net code: AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" . I have added the above statements in asp:gridview. I am getting the following error while running the application "Specified argument was out of the range of valid values. Parameter name: index " How to solve this? – user3168680 Mar 31 '14 at 06:19
  • show me your `Page_Load` code plz.. – Rahul Mar 31 '14 at 07:24
  • protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } – user3168680 Mar 31 '14 at 08:50
  • what is that,,show me your full Page_Load code and paste it on your question..not on comment. – Rahul Mar 31 '14 at 09:06

4 Answers4

0

You again you need to in page index changing event. Here you have not written that.

See the link

myGridView.DataSource = your datasource here;
myGridView.PageIndex = e.NewPageIndex;
myGridView.DataBind();
Rahul
  • 5,603
  • 6
  • 34
  • 57
Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
0

Allow paging is very simple.Call PageIndexChanging event of grid view like

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataSource = Your datasource here;
    GridView1.DataBind();
}

Hope it works for you..

Rahul
  • 5,603
  • 6
  • 34
  • 57
0

I think what I do is I write the page index code and then call the actrual bindmethod of the grid after that:

myGridView.PageIndex = e.NewPageIndex;
BindmyGridView();

where

private void BindmyGridView()
{
myGridView.DataSource = lst; //where lst is the datasource
myGridView.DataBind();
}

Also have you gone through these links:

Link 1

Link 2

Community
  • 1
  • 1
Incredible
  • 3,495
  • 8
  • 49
  • 77
0

You need to use a pageable data source. The simplest way is to just use an SqlDataSource. Also, note that PageIndex is zero-based.

If you're using a List or a Collection etc., it needs to be big enough, which is a nice waste of space. Basically, if you're doing manual paging, and you're using a List, you're going to have to create the List as big as the full record count of your result, and only fill in the proper page. Not very practical, and quite wasty, but it's simple enough.

Apart from creating your own pageable data source, there's also another, nicer way. You can use the AllowCustomPaging property to specify that you're already passing paged data. Just set VirtualItemCount to the record count, and pass just the one page in the DataSource and call DataBind as usual. That should do :)

Luaan
  • 62,244
  • 7
  • 97
  • 116