selecting row in gridview and paging

  • Thread starter Thread starter Eiriken
  • Start date Start date
E

Eiriken

Hello,

I have a result in a gridview. The result usually consists of thousands
of rows. The gridview has sorting and paging enabled. I have added an
additional column in the gridview, where users can select rows by
clicking a checkbox. The reason for this is to allow users export only
selected rows to a PDF or Excel.

This works fine if the selections are only done on one page. As soon as
users are changing to a different page, the selections made on the
previous page are gone. Thats too bad. I guess a possible solution
would be to add an event on the checkbox, and add the selections to a
collection and store them in a Session. However, that would cause a lot
of postbacks (and flickering), and is not the best solution in my
opinion. Another solution is to add selections to a collection on the
event of changing the page.

Does anyone else have a better solution for this?

Thank You.

Eiriken
 
If you are trying to actually sort, and then page and remember sort while
paging, you will have to save the manner of getting into the position you
are in between hits. You then apply the sort prior to pulling page. I would
google, as there might be someone who has conquered this and blogged.

The other option is onlyu pull X records and make a trip to the database
each time and run queries that only pull X records This is best wrapped in
custom paging and sorting.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
Thanks for your reply Cowboy.
Sorry if I didnt make myself clear. The combination of paging and
sorting works excellent, and the sortorder chosen is working too when
changing pages.

The problem I got is that I have added on extra column to the result in
GridView. The column consists of checkboxes. One checkbox for each row.
If I have selected lets say 3 rows on one page, I want these selections
to persist on the change of page. If I go one page forward, and then
back again, I want the same three rows to be selected.

I would hope there was a "automatic" way to do this, but unfortunately
I think I must program this manually to achieve this functionality.

Thanks

Eiriken
 
I would like to tell you that I found a solution for the problem I had.
If it is the best solution, I dont know, but its fast and works fine.
Anyway, if anyone else has a similar problem this is a way to solve it.

On each change of page in the grid (ChangingPage-event) go through the
rows in the gridview on the current page and check if the selection-box
is checked or not. If yes, add it to a collection. After we have gone
through all rows on the current page, add the collection to a
session-variable. Each row must of course have a unique identifier of
some kind (usually a primary key obtained from db).

While entering the next page and binding data, retrieve the
session-variable, go through each row on the page, and check if any of
the rows are in the collection. If yes, select the checkbox for that
row.

Also remember to remove rows from the collection. If a row is in the
collection, and is unchecked, remove it from the collection.

Its fast, no queries to the database, and takes up very little memory.

If anyone has a better solution, I am still happy to hear from you.

Eiriken
 
Back
Top