How to bind objects correctly with reportviewer?
I've been studying report viewer recently and I have one problem I cannot resolve...
I'm trying to bind data from a collection of objects (headers) where each object has a collection of child objects (rows). How can this be done? Below are pieces of code that I currently have (somedata is a collection of header objects).
Windows form with ReportViewer control has following:
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.LoadReportDefinition(GetReport());
reportViewer1.LocalReport.DataSources.Clear();
var dataSourcesNames = GetDataSourceNames();
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
reportViewer1.RefreshReport();
Header object:
public class ReportHeader{
readonly string id;
readonly List<ReportRow> rows;
public ReportData(Header h) {
this.id = h.Id;
rows = new List<Re开发者_Go百科portRow>();
foreach(RowObject o in h.Rows){
rows.Add(new ReportRow(o));
}
}
public string Id { get { return id; } }
public List<ReportRow> Rows { get { return rows;} }
}
Row object:
public class ReportRow{
readonly decimal sum;
readonly string type;
readonly string code;
public ReportDataRow(RowObject r) {
sum = r.Sum;
type = r.Type;
code = r.Code;
}
public decimal Sum { get { return sum; } }
public string Type { get { return type; } }
public string Code { get { return code; } }
}
I created a report that has all the properties of ReportHeader and a list which should contain all the ReportRows but it doesn't seem work. The only solution was to make two separate collections, ReportHeader collection and ReportRow collection, and then bind them separately like below:
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
var rowSource = new ReportDataSource(dataSourcesNames[1], somedata.Rows);
reportViewer1.LocalReport.DataSources.Add(rowSource);
With this solution I would need to make some kind of relation between the collections..? So please help. (note that I simplified the objects a lot for my question)
If i understand your question correctly. Could you just pass in a bool object isHeader and then in your visiability on your .rdlc text box do a function to =Fields!isHeader.value. If you have all your fields layered and hidden correctly you can have a header AND data in the same column.
精彩评论