views:

37

answers:

1

I have a grid view which has 10 rows. I have set paging = true and pageSize = 2

Now when I try to navigate through the page by the below mentioned link like 1, 2, 3 , I then receive error something like need event pageIndexChanged.

I added this event but do not understand what code should I add to this event to navigate to next page by maintain the state in each page ?

Please let me know

+1  A: 

All you need to do is set the PageIndex for the GridView to the new page, and re-bind the control.

protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
  gridView1.PageIndex = e.NewPageIndex;
  BindGrid(); // this is whatever method you call to bind your data.
}

EDIT:

You should already have an event-handler for the DataBound event of the GridView:

protected void GridView1_DataBound(object sender, EventArgs e)
{
    // lots of code here to do stuff with bound data.
}

Instead of having "lots of code", you have this:

protected void GridView1_DataBound(object sender, EventArgs e)
{
   BindGrid();
}

Therefore on the PageIndexChanging event, all you're doing is re-binding the data (calling the same logic for the DataBound event).

Make sense?

RPM1984
GridView1.DataSource = dt; GridView1.DataBind();this is the code which i write to bind my data grid on some button click. so i should re so it and how, you mean i should agian perform the data base operaion here beacue dt id in Button click event and not accessible out side
NoviceToDotNet
Refactor your code in a way that exposes dt to be accessable from the PageIndexChanging Eventhandler
citronas
How can do that so that i can get the same dt again to bind i am really novice to this please suggest me the structure i should have.because i have dt in button click.should i declare a dt out side globally but on post back it will make it emapty data table what should i do?
NoviceToDotNet
should i use viewstate or what alternate u suggest
NoviceToDotNet
i have used viewstate but the problem is that cheked check box column on navigation becomes unchecked what to do now how to resolve this
NoviceToDotNet
@NoviceToDotNet - answer edited.
RPM1984