Best way to display and Edit a 2D array in WPF
Seems like this would be easy to do, but it appears its not so simple. I have a 2d array of floats or ints and开发者_如何学Go I'd like to display it in a grid like control so it acts similar to Excel in regards to being able to move around with the arrow keys, tab keys, etc. The size of the array will vary. This comes close, but works well only for displaying:
How to populate a WPF grid based on a 2-dimensional array
I found the easiest way would be to use datatables and create one 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 in your DataTable. And also, you don't have to implement IEnumerable and stuff...
精彩评论