NSK
NSK

Reputation: 33

How to Fill GridView using more than one DataSource?

Using Multiple DataSource fill same GridView i.e I need to fill data in GridView using more than one DataSource. Please provide Code snippet if possible...

More Details:- Tables with same schema are present in two different Databases. I need to get the data from both and populate it inside one GirdView.

Upvotes: 1

Views: 5542

Answers (3)

John Eric
John Eric

Reputation:

You can combine your resultsets into one DataTable using two SqlDataAdapter to fill the DataTable from the two databases respectively. Here is an example.

        DataTable dt = new DataTable();
        using(SqlDataAdapter a1 = new SqlDataAdapter("SELECT * FROM [user1]", "Data Source=DBServer1;Initial Catalog=Database1;User ID=user;Password=***"))
        a1.Fill(dt);

        using(SqlDataAdapter a2 = new SqlDataAdapter("SELECT * FROM [user2]", "Data Source=DBServer2;Initial Catalog=Database2;User ID=user;Password=***"))
        a2.Fill(dt);

a1.Fill(dt) will initialize the DataTable and fill it. a2.Fill(dt) just adds rows to the DataTable dt from the other resultset. This example assumes that the two data sources have the same schema. If not, you have to prepare the datatable to accomodate both resultsets.

Hope this helps.

Upvotes: 1

mezoid
mezoid

Reputation: 28740

Perhaps you could combine the data from each of your datasources into a single dataset and then assign that dataset as the source of your dataview. However, without further info it's hard to speculate on possible solutions.

Upvotes: 4

Blounty
Blounty

Reputation: 3358

Are the two datasources your are wishing to use to poulate the datagrid with providing the same types of object or related objects? If you could give some extra details that would be great.

Upvotes: 0

Related Questions