3

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 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

5 Comments

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
Refactor your code in a way that exposes dt to be accessable from the PageIndexChanging Eventhandler
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?
should i use viewstate or what alternate u suggest
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.