Following on from an earlier question about dynamic columns in a report viewer, I thought I would at least attempt to get things working.
The output being displayed are columns of mathematical factors, each with their own unique name. These factors (any number from 1 to 10 can be selected) display without issue in my DataGridView control - I generate a DataTable by adding rows/columns as required, and then set the datatable as the Datasource for the DataGridView.
I have added a ReportViewer to my WinForm, and generated a RDLC file. Rather than spend hours trying to figure out how to make it dynamic, my RDLC is set up with 10 columns (as I know there will never be more than this), and the DataSet is assigned to my ReportViewer control. I then use the following code to assign the data to the viewer:
DataTable dt = new DataTable();
var source = this.dgvSelectedElements.DataSource;
while (source is BindingSource)
{
source = ((BindingSource)source).DataSource;
}
var table = source as DataTable;
if (table != null)
{
dt = table;
var reportSource = new ReportDataSource("FactorReport", dt);
this.reportViewer1.Reset();
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = "FactorReport.rdlc";
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(reportSource);
this.reportViewer1.RefreshReport();
}
So this is how the DataGridview looks:
And then, this is the Reportviewer - note the two 'Rate' and 'Mult' rows appear at the bottom
So whilst it appears to work, I cannot see the values in the grid. Also, when the ages exceed 100, the ages are sorted incorrectly - can sorting be turned off? And on the assumption I can get this to work, is it possible to iterate through the ReportViewer and amend the column captions? A quick google tells me not at run-time?