Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind datagrid to List<string[]> wpf

I have a custom data structure that is pretty much a list of string arrays that I want to display in a (virtual) datagrid in WPF. All the examples of binding I have seen have been to objects with known properties such as a Colors object with a Blue and Red property.

My data is being populated from a SQL query and returns an unknown number of columns.

How can I bind to this type of structure?

(I don't want to use something like ObservableCollection for performance reasons: my data is going to be static so I don't need INotifyPropertyChanged)

like image 253
ChandlerPelhams Avatar asked Aug 12 '11 22:08

ChandlerPelhams


1 Answers

See the following question: How to populate a WPF grid based on a 2-dimensional array

If you're only interested in displaying your 2d data then the answer from Jobi Joy will get it done using a Grid.

If you also want to be able to edit the data then you can use a control I created a while back for this purpose called DataGrid2D which subclasses DataGrid

To use it, just add a reference to DataGrid2DLibrary.dll, add this namespace

xmlns:dg2d="clr-namespace:DataGrid2DLibrary;assembly=DataGrid2DLibrary" 

and then bind it to your List<string[]> like this

<dg2d:DataGrid2D ItemsSource2D="{Binding ListStringArrayProperty}"/>
  • DataGrid2D Library
  • DataGrid2D Source
like image 78
Fredrik Hedblad Avatar answered Sep 30 '22 19:09

Fredrik Hedblad