Reputation: 16309
I have a DataGrid
I'm binding to a DataTable
, and displaying three BoundColumn
s as a result.
I would like to have a fourth column that connects with different data-- for instance, if column #3 in the DataGrid is a UserID, I would like to use that to query against a database and retrieve the user's first name, and display it in the fourth column.
Are there any recommended approaches to this? I'm not a DataGrid expert but should I be looking at a TemplateColumn
or something along those lines?
Which event should I hook all this querying to, OnItemDataBound
?
Upvotes: 0
Views: 104
Reputation: 15797
The recommended approach is return the user's first name as part of the original query and just use a BoundColumn
. If you need to modify data before displaying, then yes you can use a TemplateColumn
and the OnItemDataBound
event to manipulate your data.
Running queries OnItemDataBound
is not something I would ever encourage. So if your grid will show 100 records at once, you want to run 100 extra queries per page load? This is something that if you implement, could possibly run within reason during testing. Once under load though, you'll see have hundreds of extra queries running isn't a good idea... it can just be solved by fixing the original query or maybe making a denormalized table depending on your DB structure and the complexity of your query.
When I ran into similar issue, I went the denormalized table route.
Upvotes: 3