Best way to display and edit a 2D array in WPF

It seems like it would be easy to do, but it seems that it is not so simple. I have a 2d array of floats or ints, and I would like to display it as a grid, for example control, so it acts like Excel in regards to the ability to move with the arrow keys, tab keys, etc. The size of the array will be different. This is close, but only works well for display:

How to fill a WPF grid based on a two-dimensional array

+4
source share
1 answer

I found that the easiest way is to use datatables and create dynamically:

DataTable dt = new DataTable(); int nbColumns = 10; int nbRows = 5; for (int i = 0; i < nbColumns; i++) { dt.Columns.Add(i.ToString(), typeof(double)); } for (int row = 0; row < nbRows; row++) { DataRow dr = dt.NewRow(); for (int col = 0; col < nbColumns; col++) { dr[col] = col; } dt.Rows.Add(dr); } myDataGrid.ItemsSource = dt.DefaultView; 

Of course, this is just a random table, you can load your 2d or Xd array into your DataTable. Also, you don't need to implement IEnumerable and stuff ...

+7
source

All Articles