2

I'm currently in the process of attempting to figure out the best way to fill out my data grid columns. The grid would be displayed like this where the headings represent the days of the month and the rows represent a grouping of some sort.

       1 | 2 | 3 | 4
Foo    0   5   1   3
Bar    1   1   0   3
Foo2   3   9   7   6
Bar2   9   8   6   5

This would mean that on day 1 (of whatever month) that there were 9 of Bar2. I already have all of the data sorted and ready to go. However, I can't figure out a good way to dynamically add the days of the month as columns. I added how to do this using a binding because all of the data is in my view model and I don't really intend on adding columns using the code behind (unless absolutely necessary, of course).

Also, this does not need to use a data grid as this report is purely used for statistics and will never be modified.

I will also consider accepting an answer that offers an alternative to using a data grid.

Mike
  • 4,257
  • 3
  • 33
  • 47

2 Answers2

4

For report-style data that you want to generate on the fly, DataTable works well with DataGrid. For example, with this XAML:

<Grid>
    <DataGrid ItemsSource="{Binding}"/>
</Grid>

and this code behind:

var table = new DataTable();
table.Columns.Add(new DataColumn("Name"));
table.Columns.AddRange(Enumerable.Range(1, 4).Select(i => new DataColumn(i.ToString())).ToArray());
DataRow row = table.NewRow();
row[0] = "Foo";
row[1] = 0;
row[2] = 5;
row[3] = 1;
row[4] = 3;
table.Rows.Add(row);
DataContext = table;

we get this result:

enter image description here

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Perfect. At first I tried to figure this out in pure XAML but looking at examples of simmiliar problems it doesn't seem to be very beneficial for me. Thank you. – Mike May 05 '11 at 11:56
2

this problem reminds me of this

i think you can use the same ICustomTypeDescriptor approach to provide the desired columns/properties

on the other hand ... if it's just for some lame report, Rick Sladkey's solution looks like "less code" => easier and faster to implement

Community
  • 1
  • 1
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
  • For the time being it's simply for statistical purposes. I'm almost certain those requirements will change in the future so I've bookmarked that link. Thank you. – Mike May 05 '11 at 11:55