DatagGridViewColumn.DataPropertyName到数组元素?

我正在使用DataGridView将其数据源绑定到List,并指定每列的属性。

一个例子是:

DataGridViewTextBoxColumn colConcept = new DataGridViewTextBoxColumn(); DataGridViewCell cell4 = new DataGridViewTextBoxCell(); colConcept.CellTemplate = cell4; colConcept.Name = "concept"; colConcept.HeaderText = "Concept"; colConcept.DataPropertyName = "Concept"; colConcept.Width = 200; this.dataGridViewBills.Columns.Add(colConcept); 

{…分配其他列…}

最后

  this.dataGridViewBills.DataSource=billslist; //billslist is List 

显然,Class Bill有一个名为Concept的Property,以及每列的一个Property。

好吧,现在我的问题是,Bill应该有和Array / List / whateverdynamicsize包含称为Years的字符串容器。

让我们假设每个Bill都有相同的Years.Count,但这只在运行时才知道。因此,我不能指定像Bill.Years这样的属性来获取Bill.Years [0],Bill.SecondYear来获取Bills.Years [ 1] …等…并将其绑定到每一列。

我的想法是,现在我想要一个具有动态colums数量的网格(在运行时已知),每个列都填充了Bill.Years List中的字符串。 我可以在运行时根据Bill.Years.Count创建一个循环来向网格添加列,但是可以将它们绑定到Bill.Years List包含的每个字符串中???

我不确定我是否足够清楚。

理想情况下,结果将是这样的,列表中的2个账单,以及每个账单的3年:

 --------------------------------------网格标题---------- ---------------------
名称概念年份1年份2年份3   
 --------------------------------------网格值---------- ---------------------
 Bill1 Bill1.Concept Bill1.Years [0] Bill1.Years [1] Bill1.Years [2]
 Bill2 Bill2.Concept Bill2.Years [0] Bill2.Years [1] Bill2.Years [2]


我总是可以忘记数据源,并手动编写每个单元格,就像MSFlexGrid曾经喜欢的那样,但如果可能的话,我想使用DataGridView的绑定function。

有任何想法吗? 非常感谢。

我最近遇到了同样的问题。 我最终使用DataGridView的虚拟模式而不是绑定到数据源。 它没有与绑定完全相同的function,但它仍然比手动填充每个单元格更强大。

在虚拟模式下,DataGridView将在需要显示单元格时触发事件,这实际上意味着您可以随意填充单元格:

  private void my_init_function() { datagridview.VirtualMode = true; datagridview.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(datagridview_CellValueNeeded); } private void datagridview_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { e.Value = get_my_data(e.RowIndex, e.ColumnIndex); } 

您可以使用reflection来设置和填充DataGridView。 我用一种类型做了这个,但我不明白为什么它不能扩展到你的数据结构。

要设置DataGridView:

 // Create the columns based on the data in the album info - get by reflection var ai = new AlbumInfo(); Type t = ai.GetType(); dataTable.TableName = t.Name; foreach (PropertyInfo p in t.GetProperties()) { // IF TYPE IS AN ARRAY (OR LIST) THEN ADD A COLUMN FOR EACH ELEMENT var columnSpec = new DataColumn(); // If nullable get the underlying type Type propertyType = p.PropertyType; if (IsNullableType(propertyType)) { var nc = new NullableConverter(propertyType); propertyType = nc.UnderlyingType; } columnSpec.DataType = propertyType; columnSpec.ColumnName = p.Name; dataTable.Columns.Add(columnSpec); } dataGridView.DataSource = dataTable; 

然后填充DataGridView:

 // Add album info to table - add by reflection var ai = new AlbumInfo(); Type t = ai.GetType(); // WOULD NEED TO INCREASE BY LENGTH OF ARRAY var row = new object[t.GetProperties().Length]; int index = 0; foreach (PropertyInfo p in t.GetProperties()) { // IF TYPE IS AN ARRAY (OR LIST) THEN ADD EACH ELEMENT row[index++] = p.GetValue(info, null); } dataTable.Rows.Add(row); 

这只是我使用的代码,因此您必须修改代码以处理年份数组/列表。