将对象数组绑定到 DataGridView 控件

Posted

技术标签:

【中文标题】将对象数组绑定到 DataGridView 控件【英文标题】:Binding an array of objects to DataGridView control 【发布时间】:2020-08-03 13:00:33 【问题描述】:

我有一个基类Product 的数组,其中包含派生类MedicationParapharmaceutical 的对象。

在 DataGridView 控件中,我试图显示所有字段(基类的共同字段以及每个派生类的特定字段)。

dataGridView1.DataSource = ArrayOfProducts;

dataGridView1.ColumnCount = 6;

dataGridView1.Columns[0].Name = "Reference";

...

现在的问题是如何将数组对象的属性绑定到列(对象是不同的类型)?

【问题讨论】:

【参考方案1】:

不清楚您想在这里完成什么,但听起来您需要将“ArrayOfProducts”设置为 DataTable 以控制数据的关联具有正确的列和数据类型。这有帮助吗?

【讨论】:

是的,请问我该怎么做? 你知道你的数组的内容吗?这个线程的答案有一个简单的例子:***.com/questions/1042618/…

以上是关于将对象数组绑定到 DataGridView 控件的主要内容,如果未能解决你的问题,请参考以下文章