6

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

3
  • I can't try it at the moment...but if you just replace the button with a text box you should be able to edit the array, too. But I'm not sure how well keyboard navigation will work. Commented Oct 21, 2010 at 20:53
  • Yes, that will work as long as you also create a DependencyObject wrapper for int or float. However, like you mentioned this does not solve all the navigation and selection abilities that come with a datagrid. Commented Oct 21, 2010 at 21:20
  • Updated my answer to use the DataGrid Commented Oct 23, 2010 at 11:00

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.