【问题标题】:DataGridView, BindingList<T>, DataGridViewComboBoxColumnDataGridView、BindingList<T>、DataGridViewComboBoxColumn
【发布时间】:2008-12-12 11:29:03
【问题描述】:

所以,我有一个使用 BindingList 作为数据源的 DataGridView

DataGridView.DataSource = new  BindingList<Car>{...}

在哪里

public class Car
{
    public ColorName Color { get; set;}
}

public class ColorName
{
    public int Id {get; set;}
    public string Name{get; set;}
}

我使用了一个 Combobox 列:

DataGridViewComboBoxColumn colorNameDataGridViewTextBoxColumn;
colorNameDataGridViewTextBoxColumn.DataPropertyName = "Color";
colorNameDataGridViewTextBoxColumn.HeaderText = "Color";
colorNameDataGridViewTextBoxColumn.Name = "Color";
colorNameDataGridViewTextBoxColumn.DisplayMember = "Name";
colorNameDataGridViewTextBoxColumn.ValueMember = "Id";
colorNameDataGridViewTextBoxColumn.DataSource = new ColorName[] {...};

我怎样才能让它工作?!现在我得到一个例外,因为我认为它试图将 Id 转换为 ColorName。

我尝试使用空的 ValueMember 或将直接转换运算符添加到 ColorName 类,但无法使其工作。

当然我可以在 Car 类中使用一个 int 来表示颜色,但不是很好。

正如您可能猜到的那样,这些类实际上是 Castle Project ActiveRecord-s。

欢迎任何想法!

【问题讨论】:

    标签: .net winforms datagridview castle-activerecord


    【解决方案1】:

    您是否尝试过 ValueMember = "" 或 ValueMember = "."?

    真的很hacky,但是您可以在ColorName 上添加一个属性本身吗? (可能通过部分类)

    public ColorName Self {get {return this;}}
    

    然后设置`ValueMember = "Self";'

    除此之外,您可能还需要TypeConverter

    另一个选项可能是在ColorName 上覆盖ToString() 以返回Name,并且没有值/显示成员?


    (更新:不,它没有)

    已检查,ToString() 似乎有效:

    public override string ToString() { return Name; }
    

    只是不要设置DisplayMember ValueMember


    你知道吗 - “自我”的把戏也很有效......

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    class ColorName
    {
        public ColorName(int id, string name) {
            this.Id = id;
            this.Name = name;
        }
        public int Id { get; private set; }
        public string Name { get; private set; }
    
        // maybe declare this one in a partial class...
        public ColorName Self { get { return this; } }
    }
    class Car
    {
        public ColorName Color { get; set; }
    }
    
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            using(Form form = new Form())
            using (DataGridView grid = new DataGridView())
            {
                grid.Dock = DockStyle.Fill;
                grid.AutoGenerateColumns = false;
                ColorName[] colors = new[] {
                  new ColorName(1,"Red"),
                  new ColorName(2,"Blue"),
                  new ColorName(3,"Green")
                };
                var col = new DataGridViewComboBoxColumn
                {
                    DataPropertyName = "Color",
                    HeaderText = "Color",
                    Name = "Color",
                    DisplayMember = "Name",
                    ValueMember = "Self",
                    DataSource = colors
                };
    
                grid.Columns.Add(col);
                grid.DataSource = new BindingList<Car> {
                    new Car { Color = colors[0]}
                };
                form.Controls.Add(grid);
                Application.Run(form);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 2021-11-04
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2012-10-25
      • 2011-02-17
      相关资源
      最近更新 更多