Z
Z
ZmeuSnake2016-03-14 12:45:51
Programming
ZmeuSnake, 2016-03-14 12:45:51

How to display contents of DataTable in ReportViewer?

So far there is this code:

DataTable dt = new DataTable();
            dt.TableName = "DataTable1";
            string str = "OrderDetails " + Convert.ToString(onumber);
            SqlDataAdapter adapter = new SqlDataAdapter(str, Data.connectionString);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(dt);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            reportViewer1.Visible = true;
            reportViewer1.LocalReport.DataSources.Clear();
            ReportDataSource reportInfo = new ReportDataSource("DataTable1", dt);
            reportInfo.Value = ds.Tables[0];

            reportViewer1.LocalReport.DataSources.Add(reportInfo);
            reportViewer1.Refresh();

The code runs without errors, but the reportViewer window is empty. As I understand it, reportViewer needs to create a report to work. Add to the project, let it be report.rdlc. This report connects to the reportViewer and asks for a dataset via its visual designer. If you add a table to report.rdlc, it immediately asks you to create a dataset. You start working with the constructor, there you gradually create something like a connection string and select the tables that are needed in the report. But in my code I already have a data set with the table I need already processed, and the connection strings change depending on the login, for example, the server address, etc. The report is in the project as a separate report.rdlc file, I don’t understand how to refer to it in the code to assign a data set.
Or maybe I'm doing it all wrong.
How do I still unload my table in reportViewer1?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2016-03-14
@ZmeuSnake

Do you have a problem only with replacing the data source? Then in my opinion you have no problem - you created it yourself. Reports can be built on a typical data source based on incoming parameters. And that's pretty much what you should do. Based on the data source slipped into the wizard, a simple template with a table is generated based on the considerations that this is all that will be in the report. The data source remains the same all the time, but the parameters for generating the report are already different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question