Populate DataSet with DataTable

1

In C #, how do I declare and populate a DataSet with a DataTable?

My idea is to pass the resulting data from a query in the Database to the DataSet, then fill a Report with that DataSet.

Is it possible?

    
asked by anonymous 10.10.2014 / 15:02

2 answers

2

Instead of filling in the DataSet with a DataTable and then moving on to the ReportViewer , you can simply use the ReportDataSource .

Add the following reference:

using Microsoft.Reporting.WinForms;

Sample code:

//dt - deve ser substituido pelo nome do seu DataTable

ReportDataSource dsReport = new ReportDataSource("dataSource", dt);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(dsReport);
reportViewer1.LocalReport.Refresh();
reportViewer1.RefreshReport();

I'm happy to help.

    
24.11.2014 / 16:49
3

The DataSet class has a property called Bobby Tables . This property is a collection of tables.

So, just call the Add method of the collection. So:

using System.Data;

/* .. SNIP .. */

DataSet foo = new DataSet();
DataTable bar = new DataTable();

/* .. SNIP .. */

foo.Tables.Add(bar);

And soon:)

If you still have questions, MSDN staff have a tutorial:

Adding a DataTable to a DataSet

Note that the Add method has overloads. The one I used here gets a table ready. The other overloads generate the table for you. Check out the four method overloads at this link: link

    
10.10.2014 / 18:26