Last post Feb 07, 2012 07:39 AM by Qin Dian Tang - MSFT
Feb 02, 2012 01:54 PM|c_borgman|LINK
I have created a grid view that groups records into a group row that is expandable. In the application geach group may have 3 to 5 records and there are about 50 groups. 50 groups means that when the page loads there are 50 rows displayed, however I would
like this to be 10 rows. This means I need to implement some sort of paging which is what I want to do.
The way the grouping works is the data srouce is sorted by the field I want to group by. Then as I am binding each row, if the current row being bound has a different group by field value than the previous row I write in a group header row for that group.
After all the rows are written as the grid view is being rendered I hide all "child" rows with a display: none style. There is an image button in the group header row that toggles display to visible or none depending on the current display value of the child
rows. This all works fine and good.
Now for my problem. When I implement paging I am trying to show 10 group header rows per page. The problem is that only the child rows are being counted. So since lets say the first 3 groups have 4 records each and when using the default page size of
10 I only get 3 group headers and the 3rd group only has half the children (4+4+2 = 10 records). The problem gets further complicated when groups have variable amounts of records (3 to 5 as stated earlier)
Frankly I have been unable to change the page size in the code behind at all much less figure out how to do a variable page size. My question is, how can I programatically set the page length for each page diferently to show 10 group headers. Or at least
what might be the pest place in the life cycle to try to do this.
Any pointer or suggestions would be greatly appriciated.
Feb 02, 2012 02:09 PM|superguppie|LINK
Instead of having a single GridView, you should use Nested GridViews. The outer one will get only the Groups. In that you can easily do paging. The inner will get the group from the Row it is in, and bind the data for that group.
In the naive solution, that will mean a query for each group. And that can be costly. Depending on the situation there are ways to deal with it. But if the form is fast enough with the naive solution, it's ok to stick with that.
If you need a less naive solution, (get data in one go, and have different Controls use them) I can probably help with that.
Feb 02, 2012 02:16 PM|c_borgman|LINK
That was the problem with why I am using the solution I have now. The solution really should be done with one query. 50 queries just for a grid view will be way to taxing in the long run. I am also trying to keep the solution so that I can drop in implement
it for future projects. I havent implemented a search feature but I figure I haven't needed to cross that bridge yet so why do it now.
What would you regaurd as the best way to implement dynamic "child" gird views (or data grids) then?
Feb 02, 2012 02:26 PM|superguppie|LINK
I'm not sure it will work in your case, but in a recent project I built the following:
I made a class (say Question) representing a single Item in the outer GridView.
I made another class (say AnswerOption) representing a single Item in the inner GridView.
I then gave Question a member that was a List<AnswerOption>/List(Of AnswerOption)
I could get the data in one go, and sort it into instances of the respective classes.
Then I could bind the outer GridView to a List<Question>/List(Of Question) and set the DataSource of the inner GridView to the AnswerOptions of the Question that is the DataItem of the GridViewRow in the RowDataBound handler of the outer GridView.
Is that an idea for you?
Feb 02, 2012 02:33 PM|c_borgman|LINK
That is interesting. I am already using a List(Of Job) so let me fiddle a bit and get back to yo with what I find. The only trick to this is going to be hiding the values when I dont want to see them. I'll keep you posted.
Feb 07, 2012 07:39 AM|Qin Dian Tang - MSFT|LINK
You need to use nested GridView. For paging it is in the outer GridView, so the datasource is from the parent GridView, not nested GridView. I think if you separate the datasource of nested GridView, it won't mismatch.