merge two datasource to gridview?

2019-08-14 04:52发布

what is the best way of doing?, i have one table that returns one set of data and another table that returns another set of data, so in my gridview i want to display

id,name = dataset1 registration_id, registration_name = dataset2

gridview looks like this:

id | name | reg_id | reg_name |

2楼-- · 2019-08-14 05:21

Best bet is to use a JOIN when selecting the data from the database.

If that's not possible, there are several ways to accomplish this with DataTable objects in memory...

One possibility is outlined here: This is how I usually do it if I absolutely can't just get it directly from the server in the format I want.

You can also do it using Linq

An example of a JOIN with LINQ can also be found here:

Edit - added based on comments

Based on your comments, I'm not sure that joining the results in a gridview is necessarily what you want. In a one-to-many relationship you will get duplicaiton from the "one" side.

If I'm guessing right, what you really want is something that more accurately represents the one-to-many relationship so instead of data that looks like this:

id | name | reg_id | reg_name |
1  |abs   | 1      |adad      |
1  |abs   | 2      |sadsd     |

you really want it to look like this:

1  abs
   1   adad
   2   sadsd

In that case, you're better off looking into Nested Repeaters: or another way to represent heriarchical data.

3楼-- · 2019-08-14 05:22

I think in addition to all the great previos methods , you can write a stored procedure,which contains your logic (you can use temporary tables #table),make it the grid view data source..i use stored procedures many times when i need data from many tables to avoid complex joins and nested controls which may cause low performance according to what i know.

if u mean u use two data sources for the same grid view but the source changes according to some condition , this will help u ..

gridview with more than one data source

登录 后发表回答