views:

23

answers:

2

I have a GridView that is bound to a select statement from a table. That table contains a lot of keys out to other tables that are just IDs. I would like the GridView to "dereference", as it were, the id of the field in question and display the human-readable name found in the other table.

At the moment, the options that come to my mind are composing a DataSet by hand in the codebehind and binding the GridView to that instead of the SqlDataSource or creating a stored procedure to return the table already "dereferenced". Any other ideas or recommendations for this situation? I am using .NET 2.0 per employer mandate.

+1  A: 

I am not entirely sure what you mean by "dereferencing" but if you just want to display the value from the tables refered to by foreign keys then just bind your gridview to a query that joins all the required tables in a flat dataset. e.g.:

SELECT Table1.Field1, Table1.Field2, Table2.Field1, Table2.Field2
FROM Table1
LEFT OUTER JOIN Table2 on Table1.Table2ID=Table2.ID

This can be done either in a stored procedure or by directly specifying this as the SelectCommand property.

Ben Robinson
A: 

I just ended up creating a view that automatically dereferenced the ids to the other tables and using that for the basis of the GridView.

cookiecaper