How to populate a GridView using multiple DataSource?

Using Multiple DataSource populates the same GridView. ie I need to populate data in a GridView using more than one DataSource. Please provide a code snippet if possible ...

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

+1


source to share


3 answers


Perhaps you could combine data from each of your data sources into a single dataset and then designate that dataset as the source for your document. However, without additional information, it is difficult to speculate on possible solutions.



+4


source


You can combine your result objects into one DataTable by using two SqlDataAdapters to populate the DataTable from the two databases respectively. Here's 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) initializes the DataTable and fills it. a2.Fill (dt) just adds rows to DataTable dt from another result set. This example assumes that two data sources have the same schema. If not, you must prepare the data to accommodate both sets of results.

Hope it helps.

+1


source


Are the two datasources you want to use to strip away the datagrid while providing the same object types or related objects? If you could give more details, that would be great.

0


source







All Articles