Fill Dataset with Datatable

Asked

Viewed 2,679 times

1

In C#, how can I declare and fill a Dataset with a Datatable?

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

It is possible?

  • 2

    Yes, it’s completely possible. What you’ve achieved so far?

  • 2

    Read this and put your question to the community standard: http://answall.com/help/mcve

2 answers

3

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

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

using System.Data;

/* .. SNIP .. */

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

/* .. SNIP .. */

foo.Tables.Add(bar);

And just like that :)

If you still have any questions, the MSDN staff has a tutorial:

Adding a Datatable to a Dataset

Note that the method Add has overloads. The one I used here receives a table already ready. The other overloads generate the table for you. Check out the four overloads of the method in this link: http://msdn.microsoft.com/pt-br/library/system.data.datatablecollection.add(v=vs.110). aspx

  • Renan, in this case I can use this Dataset to fill the Reportviewer?

  • 1

    @Arielcardoso, in cases like yours the most important question to ask is: why not? : D Seriously now, I never used Reportviewer, but if all it expects is a Dataset, it should work yes.

2


instead of you filling the Dataset with a Datatable and then move 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();

Smart to have helped.

  • years later I saw his answer. And that’s exactly how I did it. D

  • hahaha, perfect!! Strong hug and good luck brother

Browser other questions tagged

You are not signed in. Login or sign up in order to post.