DatagGridViewColumn.DataPropertyName数组元素?数组、元素、DatagGridViewColumn、DataPropertyName

2023-09-03 17:28:50 作者:离殇陌路

我用一个DataGridView其数据源绑定到一个列表,并为每列指定的属性。

I'm using a DataGridView binding its datasource to a List, and specifying the properties for each column.

一个例子是:

 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);

{...指定其他colums ...}

{... assign other colums...}

最后

 this.dataGridViewBills.DataSource=billslist; //billslist is List<Bill>

显然,比尔类有一个属性叫做概念,还有一个属性为每个列。

Obviously Class Bill has a Property called Concept, as well as one Property for each column.

好了,现在我的问题是,比尔应该有,并呼吁多年字符串数组/列表/ whateverdynamicsizecontainer。

Well, now my problem, is that Bill should have and Array/List/whateverdynamicsizecontainer of strings called Years.

让我们假设每一个条例草案将具有相同的Years.Count,但这仅知在runtime.Thus,我不能指定属性,如Bill.FirstYear获得Bill.Years [0],Bill.SecondYear取得票据.Years [1] ...等等,并将其绑定到每一列。

Let's assume that every Bill will have the same Years.Count, but this only known at runtime.Thus, I can't specify properties like Bill.FirstYear to obtain Bill.Years[0], Bill.SecondYear to obtain Bills.Years[1]... etc... and bind it to each column.

我们的想法是,现在我想和柱的侧向承载力的动态数(在运行时已知的),并提供了从Bill.Years列表的字符串每列一个网格。我可以使一个环将列添加到根据Bill.Years.Count的网格在运行时,但有可能将它们绑定到每个所述Bill.Years列表中包含???弦

The idea, is that now I want to have a grid with dynamic number of colums (known at runtime), and each column filled with a string from the Bill.Years List. I can make a loop to add columns to the grid at runtime depending of Bill.Years.Count, but is possible to bind them to each of the strings that the Bill.Years List contains???

我不知道如果我不够清晰。

I'm not sure if I'm clear enough.

结果理想情况下是这样的,2钞票就行了,3年为每个账单:

The result ideally would be something like this, for 2 bills on the list, and 3 years for each bill:


--------------------------------------GRID HEADER-------------------------------
NAME      CONCEPT         YEAR1              YEAR2                YEAR3   
--------------------------------------GRID VALUES-------------------------------
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的绑定功能。

I can always forget the datasource, and write each cell manually, as the MSFlexGrid used to like, but if possible, I would like to use the binding capabilities of the DataGridView.

任何想法?非常感谢。

推荐答案

我最近遇到了同样的问题。最后我用的DataGridView的虚拟模式,而不是绑定到数据源。它不具有完全相同的功能具有约束力,但它仍然是一个很多比手动填充每个单元更加强大。

I recently ran into this same problem. I ended up using DataGridView's virtual mode instead of binding to a data source. It doesn't have exactly the same features as binding, but it's still a lot more powerful than populating each cell manually.

在虚拟模式下,在DataGridView会触发一个事件时,它需要显示的小区,基本上意味着你可以但是填充单元格请您:

In virtual mode, the DataGridView will fire an event whenever it needs to display a cell, which essentially means you can populate the cell however you please:

    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);
    }
 
精彩推荐
图片推荐