Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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

like image 536
AKoran Avatar asked Oct 21 '10 20:10

AKoran


1 Answers

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...

like image 193
user627283 Avatar answered Nov 08 '22 01:11

user627283