views:

96

answers:

1

Tell me if this is even possible:

I have a gridview bound to an ObjectDataSource providing me with data from a database. I'm filling the Objectdatasource using Linq-to-entities. Typical setup where I'm supplying the columns and rows.

Below I have a horrible graphical representation.


       x_col    x_col   x_col   
x_row| x_data   x_data  x_data    
x_row| x_data   x_data  x_data  
x_row| x_data   x_data  x_data  
x_row| x_data   x_data  x_data    
x_row| x_data   x_data  x_data   

What I would like to do is add columns to this gridview from another table in the database: orginal data being x; different data being y.


       x_col    x_col   x_col   y_col   y_col   y_col 
x_row| x_data   x_data  x_data  y_data  y_data  y_data
x_row| x_data   x_data  x_data  y_data  y_data  y_data
x_row| x_data   x_data  x_data  y_data  y_data  y_data
x_row| x_data   x_data  x_data  y_data  y_data  y_data  
x_row| x_data   x_data  x_data  y_data  y_data  y_data 

I don't know any of the y info until runtime, so that is added dynamically. I don't need to update x back to the database, but I do need to update y.

It looks like the datasource can only have once select method, so I don't know how I can do this.

I hope I'm not being too abstract here.

+1  A: 

The short answer is yes, you can dynamically change/regenerate the columns of a GridView. The easiest way to do so is to programatically define the DataSource before DataBinding. The DataSource can be an IEnumerable of practically any object, allowing you to create a custom GridRow object that holds the data in the form you wish to view it.

Here's what I would do, at a high level:

  • Remove the static column definitions from your GridView in the markup, and set the property AutoGenerateColumns to true on the GridView itself. This will cause the GridView to create columns on DataBinding based on the object used as the DataSource.

  • Create a simple, POCO class to represent each set of data you want to show. If the number of different column arrangements is going to be large, or cannot be known at compile-time, it may be worth the overhead to pull or push the data into a DataTable.

  • When preparing your results, transfer the data into a list of the applicable POCO class, or into the DataTable. Set this List or DataTable as the DataSource, and call DataBind()

  • Custom labels can be specified programmatically; the system will default to the ToString() representation of the field, property or column name of its DataSource object. You can override this behavior by attaching a handler to the OnDataBinding event of the GridView, or simply changing column names after calling DataBind().

Alternately, you can choose to not create columns automatically, but still do so dynamically on page load or PreRender. This will allow you to control all the specifics yourself, without relying on defaults. You can use this to create various dynamic grids that will all map to fields of the same data object, reducing your class count somewhat.

Dynamic grid creation is useful for things like search pages, where you may want to look for one type of record out of many, but don't want to have a pre-defined GridView for everything you're able to search for, nor have to normalize the search results so they all fit in a similar grid.

KeithS
Thanks for the reply! Have you found any examples of this that I could take a look at?
isorfir