tags:

views:

51

answers:

1

Hi,

I have a collection of structures. The structure is just some strings. Example public struct ReportLine { public string Name; public string Address; public string Phone; ...// about 10 other strings } I can't change this part. What I want to do is display it in a simple grid or simlar in WPF. My only requirements are: a) need column headers b) rows must alternate in color c) columns big enough to hold largest datum (which is not know until run time)

Can someone point me to an example to get me started? Is the GridView the way to go? Or DataGrid? Or perhaps just the grid?
I have the book Pro WPF in C# 2008 and it covers binding ListBox's to collections, but the collections always seem to be collections of one field (ex. a collection of 40 names). Here I have a collection (an array in fact) of a structure. How do I setup the databinding?

As you can see, I'm new to this and probably would most benefit from a reference to an article. I've found articles covering collections of 1 field, but no examples covering binding to an array of structures. Also, my intitial research indicates c) can't be easily done, if you demand that the column is big enough for all the data in that column, not just the visible data.

Thanks, dave

+1  A: 

All of the above can be done with the DataGrid and the ListView. Preferably in conjunction with DataTemplates.

Its pretty simple, for example all requirements you listed can be achived with this single line:

<DataGrid AutoGenerateColumns="True" 
          ItemsSource="{Binding Items}" 
          AlternatingRowBackground="Gray" />

Where Items ist a property of type Item[] (an array of your structure instances).

Note that the strings need to be exposed as propeties not as fields. If your structure does not meet this requirement you will have to introduce an adapter class or structure for this. Databinding generally does not work against fields.

bitbonk
Thanks! That worked great. For those following this thread later, note that I had to download WPF Toolkit to get the DataGrid. I'm on VS2008, this may not be the case with VS2010
Dave