Question

WinForms reportViewer (.NET 3.5) binding data from a DataTable

I'm trying to bind a DataTable which is dynamically created, to a reportViewer control . There are many tutorials on this on the internet but they don't seem to work for this project... (there are related questions on stack overflow but the solutions don't work in my case)

This is the event handler for the Show Report Button:

    private void btn_ShowRwport_Click(object sender, EventArgs e)
    {
        //Set the date range for the report
        rptctn.SetDateRangeOne(listBox1.SelectedItem.ToString(), dateTimePicker_Start.Value, dateTimePicker_End.Value);

        //Get the report datatable
        DataTable dt = rptctn.GetReportTest(listBox1.SelectedItem.ToString());

        //Set the datagridview
        dataGridView1.DataSource = dt;

        //Set the reportViewer
        this.reportViewer1.LocalReport.DataSources.Clear();            
        Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt);
        this.reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
        this.reportViewer1.RefreshReport();
    }

I know the DataTable is being retried properly as the dataGridView works... https://docs.google.com/file/d/0B6b_N7sDgjmvZHpEYS1BWWhqZ3c/edit?usp=sharing

The project has the following constraints:

  • Must use .NET 3.5 due to client environment. (so report viewer 2008)

  • Creation of DataTable and the ReportViewer are in different packages (application/presentation layer)

  • I'm trying to avoid creating .rdlc files as reports (DataTables) have a variable number of rows/columns.

Anyone know how i can bind a dynamicly created DataTable to a report viewer in my case?

Était-ce utile?

La solution

Really what you're asking to do is dynamically create an RDLC file and add a table. I found a 3rd party example with some code from gotreportviewer.com, just look down on the right hand side of the page for Generate RDLC dynamically - Table.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top