1

I've got a list of name-value pairs that I'm wanting to put in a nice report format. Is it possible to use this as a data source for a ReportViewer object? This is in WinForms and ASP.

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
  • Is the `ReportViewer` a WPF control? If so, I would imagine you could achieve what you want in the same way you would through standard item binding. If not, maybe this question is related: http://stackoverflow.com/questions/837625/using-ms-reportviewer-in-wpf – Samuel Slade Dec 16 '11 at 14:57

4 Answers4

3

I was able to convert the Dictionary to a DataTable and use that as the DataSource.

var table = new DataTable();
var kvps = dictionary.ToArray();
table.Columns.AddRange(kvps.Select(kvp => new DataColumn(kvp.Name)).ToArray());
table.LoadDataRow(kvps.Select(kvp => kvp.Value).ToArray(), true);
bindingSource.DataSource = table;

Voilà

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
  • how do you refer to a well named key (column/row) of the data source inside the rdlc design mode ? I have tried `=Fields("somekey")!FirstName.Value` and `=Fields!FirstName.Value("somekey")` whithout success – Bellash Mar 18 '16 at 10:04
  • @Bellash sorry, it's been too long ago and I don't remember. – Dax Fohl Mar 18 '16 at 13:13
0

I believe you'll have to create a class to encapsulate the items you'd like to display in the report. See my answer to this question for how to bind a class to a report.

The code's in VB.NET but you'll be able to follow it quite easily.

Community
  • 1
  • 1
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
0

I don't think that's not going to naturally bind in the ReportViewer. Just put them in a list of custom objects with the properties you want to bind to. It wouldn't be a lot of overhead (like a List.

Jared Peless
  • 1,120
  • 9
  • 11
0

Dictionaries are not suited as data source for lists. They are optimized for retrieving values for given keys. Nevertheless, you can retrieve the keys, the values and key/value pairs from the dictionary as follows:

var keys = dict.Keys;
var values = dict.Values;
var keyValuePairs = dict.OrderBy(x => x.Value);
foreach (KeyValuePair<string, int> item in keyValuePairs) {
    Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188