Fetching data from different table in asp.net
I want to fetch data from separate tables in sqlserver and displ开发者_StackOverflow中文版ay them together in GridView, Is that possible? if so then please suggest. Thank you.
Yes, you can do that. Sample SQL is :
SELECT * FROM firstTable UNION ALL SELECT * FROM secondTable
Then you can get the results to datatable and bind that datatable to gridview.
Yes, that is possible, if both the returned result schemas are the same.
You would have to either join them at SQL level (with UNION
) or at code level (with Enumerable.Union()
).
Bottom line, you can only assign a single datasource to a control.
I can think in two ways of do it, the first is that the SQL query join the data in the desired way and return it merged.
The other is making a query that returns a DataSet with the 2 tables and merge in c# creating a third datatable via code with the columns we want and populating later from the obtained data.
I will use the first option if is posible..
精彩评论