开发者

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

开发者 https://www.devze.com 2023-01-21 14:59 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消