简体   繁体   English

在 DataGridView 中显示集合包装类的 ToString

[英]Show ToString of a Collection Wrapper Class in DataGridView

I have a class that has one of its properties being of type ObservableCollection<string>我有一个类,其属性之一是ObservableCollection<string>类型

public class SizeList
{
    public string ID { get; set; }

    public string Name { get; set; }

    //public ObservableCollection<string> List { get; set; }
    public ListEntryCollection List { get; set; }
}

During a unit test I return a list of SizeList and then show it in a DataGridView to check the results I am expecting, the data is fine but I am missing the field List in the DGV;在单元测试期间,我返回一个SizeList列表,然后在DataGridView中显示它以检查我期望的结果,数据很好,但我缺少 DGV 中的字段List only the ID and Name are shown, so I have made a wrapper class for the ObservableCollection<string> and overriden its .ToString() method:仅显示 ID 和名称,因此我为ObservableCollection<string>创建了一个包装类并覆盖了它的.ToString()方法:

namespace System.Collections.ObjectModel
{
    using System.Collections.Generic;

    public class ListEntryCollection : ObservableCollection<string>
    {
        public ListEntryCollection(IEnumerable<string> collection)
            : base(collection)
        {
        }

        public override string ToString()
        {
            return Count.ToString() + ((Count > 1) ? " Entries": " Entry");
        }
    }
}

But I am still not getting the List field in the DGV, so what am I doing wrong?但是我仍然没有得到 DGV 中的List字段,那么我做错了什么?

DGV 结果

Per Column Types in the Windows Forms DataGridView Control , the only property types for which bound columns are automatically generated are numbers, text, booleans, and images. Windows 窗体 DataGridView 控件中的每列类型,自动生成绑定列的唯一属性类型是数字、文本、布尔值和图像。

To display other types, you need to add the column manually to the DataGridView , or use a custom column type (and even then, you'll probably have to add it manually.)要显示其他类型,您需要手动将列添加到DataGridView ,或使用自定义列类型(即便如此,您可能还必须手动添加它。)

A couple of options present themselves:有几个选择:

  • You could try adding a read-only property to SizeList to display the description of the list, and see if that will result in a column being automatically created.您可以尝试向SizeList添加只读属性以显示列表的描述,并查看这是否会导致自动创建列。

  • You can try adding a column manually, which I believe you can do in the Form Designer if you click on the DataGridView .您可以尝试手动添加一列,我相信如果您单击DataGridView ,您可以在表单设计器中执行此操作。 You will probably have to override a method or two, or use an event handler, in order to change the display from the default.您可能必须重写一两个方法,或使用事件处理程序,才能更改默认显示。 (It's possible, though, that it will use the ToString override you created, in which case the problem is solved.) (不过,它可能会使用您创建的ToString覆盖,在这种情况下问题就解决了。)

  • Or, you could create a ListSummaryDataGridViewColumn class, that can represent a list by display a count of the items in it, and add one of those manually.或者,您可以创建一个ListSummaryDataGridViewColumn类,它可以通过显示其中的项目计数来表示列表,并手动添加其中一个。

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

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