【问题标题】:C# array/list into DataGridView ColumnC# 数组/列表到 DataGridView 列
【发布时间】:2014-07-24 23:01:47
【问题描述】:

我的代码如下所示:

public class A
    {
        public string N{ get; set; }

        public int E1 { get; set; }

        public int E2{ get; set; }

        public List<Genre> G{ get; set; }

        [System.ComponentModel.Browsable(false)]
        public string L { get; set; }
    }

我有一个 BindingList 包含其中的许多。现在我想在 DataGridView 中显示这个列表,效果很好,只是 A 中的 List 没有显示。

绑定数据源:

dgSeen.DataSource = Storage.seen;

列表 G 会在一段时间后设置,并具有正确的值。 所以我想要的是,有一个第四个 DataGridView 列向我显示列表的内容,如“Action、Fatasy、SciFy”等等。

我希望您了解我的需要,并提前感谢您的每一个帮助:)

【问题讨论】:

    标签: c# arrays list datagridview


    【解决方案1】:

    您可以为此目的创建一个计算属性。

    public string GDisplay 
    {
        get 
        {
            return String.Join(", ", G);
        }
    }
    

    然后在你的网格中使用GDisplay来代表G。显然这是一个只读的设计。如果您希望能够通过网格修改 G 的值,则必须为GDisplay 实现一个设置器。例如:

        set 
        {
            G = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                     .Select(x => x.Trim())
                     .ToList();   
        }
    

    【讨论】:

    • thx,效果很好:) 我不需要编辑它,所以不需要那个
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-04-20
    相关资源
    最近更新 更多