One gridview – two datasources

asp.netdatasourcegridview

I am working on a co-workers website, and he mistakenly put up a gridview that shows a TypeID instead of the TypeName. I want to put the actual NAME of the type, instead of it's arbitrary ID number, like he has it. I see he has two "ObjectDataSource"'s on the page — one for Type and one for Item. Items contains the ID of what TYPE the item is, which is all in the Items datasource. He is pulling this ID to populate the gridview. I want to use that ID to pull the NAME form the other data source, and place that NAME into the gridview for that specific column. Can this be done? Can I use two different datasources on a gridview?

Best Answer

If i'm understanding this right, what you have is two datasources that do this:

1) SELECT ID, Item, ItemTypeID FROM Item
2) SELECT ID, ItemType FROM ItemTypes 

And you're wanting the Item name and the Item Type displayed in your grid.

To answer your fundamental question if you can use both sources on the grid: yes. But that's not the most efficient way to perform the operation, as you'd need to be walking the second datasource for every line in the first.

A better way would be to join the two datasources together, and only have one.

Modify the SELECT statement for the first datasrouce to be something along these lines:

SELECT i.ID, i.Item, t.ItemType FROM Item i INNER JOIN ItemTypes t ON i.ItemTypeId = t.ID;
Related Topic