views:

3159

answers:

4

I have a scenario wherein, for example, I need to repeat a list of US states and display a table of cities and city populations after the name of each state. The design requirement dictates that every outer repetition must be the name of a state followed by a table of cities, and that requirement cannot be changed at this time. Are there disadvantages to nesting a GridView within a Repeater and then binding each repeated GridView during the Repeater's ItemDataBound event? What are some alternative solutions?

+2  A: 

If it were me, I'd reverse the question and ask why I should use a GridView, If you need a bunch of built-in features like paging and sorting, then the GridView might be a good fit. If you just want tabular data, I'd reconsider. Why? Because with GridView you're getting a whole bunch of stuff you won't use, your ViewState will be potentially huge, and your page performance will be slower.

I'm not a bigot when it comes to GridView, but I only use them when there is a damn good reason.

CarmineSantini
If I forgo using a GridView, do you sugguest I use another Repeater to build my <table> HTML or loop through the dataset in the codebehind and spit my output into a Literal?
Bill Ayakatubby
If code behind, I would add to your table with controls (HtmlTableRow, HtmlTableCell) instead of building a string and dumping it into a literal (if that's what you meant). Or use HtmlTextWriter. I don't like to do presentation in code-behind, so if it were me, I'd use a Repeater or ListView.
CarmineSantini
+1  A: 

In your above scenario, you'd be better off doing a master-detail style GridView, which will save you the overhead of all those GridView objects that get created.

There are various implementation of it (using a drop down for the master, using a modal popup for the detail, etc.), but the main point is that there are implementations available.

Dillie-O
Unfortunately, the design requirement dictates that every outer repetition must be the name of a state followed by a table of cities. I've edited my post to make that point clear.
Bill Ayakatubby
+1  A: 

At the very least, hopefully you can turn off ViewState on the GridViews.

Alison
+1  A: 

The best solution I was able to come up with was to nest the GridView in the Repeater. Then I bound each repeated GridView during the Repeater's ItemDataBound event. I turned off their ViewStates, of course, as they weren't required.

Bill Ayakatubby