我可以将多维数据绑定到c#和。net中的DataGridView吗?

时间:2020-12-03 21:35:32

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中子类化的工作表。用户可以将(或导入)类csv的数据粘贴到工作表中,并将其反映到内存中的数据结构中——我的第一个猜测是一个二维的浮点数组。

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

DataGridView可以绑定到具有特定接口集的对象(例如IList、IListSource等),因此,理论上我可以创建一个类,该类封装一个2D数组并实现其中的一个接口。然而,令人困惑的是接口规范似乎只满足于一维数组——例如,参见IList。什么是怎么回事? !

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?

更新:从答案来看,IList似乎在寻找对象列表。是否有一种方法可以将任意大小(浮点数)的多维数组绑定到DataGridView?或者可以使用DataGridView本身作为存储浮动的数据结构吗?

3 个解决方案

#1


3  

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

我之前做过类似的事情,在这里——在IList中表示一个二维数组;可能是有用的。

#2


2  

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:

这样想。IList可以是对象的集合。然后,每个对象可以在其中包含多个属性。本质上,这就像一个二维数组。第一个维度是对象,它的所有属性,第二个维度是所述对象的集合。是这样的:

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.

因此,在您的例子中,如果列是固定的,那么您只需要有一个对象来封装所有这些字段,然后有这些对象的集合,然后绑定到DataGridView。

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

如果这没有任何意义,那么我不理解你的问题,我道歉。

#3


2  

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

在CodeProject上有一篇不错的文章,介绍了如何将2D数组绑定到DataGridView。

#1


3  

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

我之前做过类似的事情,在这里——在IList中表示一个二维数组;可能是有用的。

#2


2  

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:

这样想。IList可以是对象的集合。然后,每个对象可以在其中包含多个属性。本质上,这就像一个二维数组。第一个维度是对象,它的所有属性,第二个维度是所述对象的集合。是这样的:

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.

因此,在您的例子中,如果列是固定的,那么您只需要有一个对象来封装所有这些字段,然后有这些对象的集合,然后绑定到DataGridView。

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

如果这没有任何意义,那么我不理解你的问题,我道歉。

#3


2  

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

在CodeProject上有一篇不错的文章,介绍了如何将2D数组绑定到DataGridView。