views:

2019

answers:

3

I'm looking to develop an app which features worksheets subclassed from DataGridView. Users can paste (or import) CSV-like data into the worksheet and it will be reflected in a data structure in memory - my first guess would be a 2D array of floats.

DataGridView can be bound to objects with a certain set of interfaces (i.e. IList, IListSource etc.) and so, in theory I could create a class which encapsulates a 2D array and implements one of these interfaces. However, what is puzzling is that the interface specs seem to only cater for 1 dimensional arrays - see IList for example. What is going on?!

Update: From the answers, seems IList caters for lists of objects. Is there a way then to bind a multi-dimensional array of arbitrary size (of floats) to a DataGridView? Or is it ok to use the DataGridView itself as the data structure for storing the floats?

+1  A: 

Think of it this way. An IList can be a collection of objects. Each object can then have multiple properties within them. So essentially, that's like a 2D array. The first dimension is the object, and all of it's properties, and the second dimension, is a collection of said objects. Something like this:

list[0] --> Name, (think of this as list[0]["Name"])
            Age, (think of this as list[0]["Age"])
            Height  (think of this as list[0]["Height"])


list[1] --> Name, (think of this as list[1]["Name"])
            Age, (think of this as list[1]["Age"])
            Height  (think of this as list[1]["Height"])

So, in your case, if the columns are fixed then you just need to have one object that encapsulates all those fields and then have a collection of those objects that will then be bound to the DataGridView.

If this doesn't make any sense, then I didn't understand your question and I apologize.

BFree
Yup that makes sense, have modified question
Brendan
+1  A: 

There's a nice article on CodeProject showing how to bind 2D arrays to a DataGridView.

Darin Dimitrov
+1  A: 

I've done something like this before, here - representing a 2D array in an IList; might be useful.

Marc Gravell
It's a good idea but not quite there - I'm working with data that may resize in either dimension i.e. a column may be deleted just as readily as a row
Brendan
That can in theory be simulated, but the grid control won't help you with it.
Marc Gravell