简体   繁体   English

C#WinForm DataGridView绑定列表中的列表

[英]c# WinForm DataGridView bind List in List

I want to bind list (ProductList) to DataGridView, but one column is collection (Categories), but the DataGridView show "(Collection)" and not the content of List. 我想将列表(ProductList)绑定到DataGridView,但是一列是集合(Categories),但是DataGridView显示“(Collection)”而不是List的内容。 I can't rewrite or change this ProductList class. 我无法重写或更改此ProductList类。 How can i bind this Categories List column? 如何绑定此类别列表列?

Here is the Binding: 这是绑定:

dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = model.ColumnsNames.Length;
for (int i = 0; i < model.ColumnsNames.Length; i++)
{
    string columnName = model.ColumnsNames[i];
    dataGridView1.Columns[i].Name = columnName;
    dataGridView1.Columns[i].DataPropertyName = columnName;
}
dataGridView1.DataSource = model.Products;

Here is ProductModel and the data source of grid is List<Product> : 这是ProductModel ,网格的数据源是List<Product>

public class Product
{
    //...

    /// <summary>
    /// List of product categories names (string). 
    /// In write-mode need to pass a array of categories IDs (integer)
    /// (uses wp_set_object_terms())
    /// </summary>
    public List<object> categories { get; set; }

    // ...
}

I don't want to use sub-grid, i just want to show the property as a string in a TextBoxColumn something like this: CollectionElement1, CollectionElement2, etc. 我不想使用子网格,我只想在TextBoxColumn中以字符串形式显示属性,如下所示:CollectionElement1,CollectionElement2等。

It's not my class, actually just a reference. 这不是我的课,实际上只是参考。 So I cant change it anywhere. 因此,我无法在任何地方进行更改。

You can use CellFormatting event of DataGridView to provide a friendly representation of categories property: 您可以使用DataGridView CellFormatting事件来提供categories属性的友好表示形式:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //Use zero based index of the cell that contains categories
    var cell= 3;

    if (e.ColumnIndex == cell && e.RowIndex >= 0)
    {
        var categories = (List<object>)dataGridView1.Rows[e.RowIndex].Cells[cell].Value;
        e.Value = string.Join(", ", categories.Select(x => x.ToString()));
    }
}

In this case, categories contains List of product categories names (string). 在这种情况下, categories包含List of product categories names (string). So selecting ToString() is OK. 因此,选择ToString()是可以的。 But for a case of List<SomeClass> , you either should override ToString of SomeClass or select one of it's properties. 但是对于List<SomeClass> ,您应该覆盖SomeClass ToString或选择其属性之一。

As another option you can shape the result of query using a new ViewModel or using anonymous type, but in the current situation which Product model doesn't belong to you and it has lot's of properties, previous solution is the most simple option. 作为另一个选择,您可以使用新的ViewModel或使用匿名类型来确定查询的结果,但是在当前Product模型不属于您并且它具有很多属性的情况下,以前的解决方案是最简单的选择。

Something like 就像是

dataGridView1.DataSource = (from p in model.Products 
                            select string.Join(", ", p.categories.ToArray())).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM