DataGridView、BindingList、DataGridViewComboBoxColumn

发布于 2024-07-10 09:02:25 字数 1068 浏览 3 评论 0原文

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

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。

欢迎任何想法!

So, I have a DataGridView using as datasource a BindingList

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

Where

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

with

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

and I use a Combobox column:

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

How can I get this to work ?! Now I get an exception because I think it tries to cast the Id to ColorName.

I tried with an empty ValueMember or adding a direct cast operator to ColorName class but can't get it to work.

Sure I can use an int in the Car class to represent the color but is not as nice.

As you probably guessed those classes are in fact Castle Project ActiveRecord-s.

Any ideas are welcome !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-07-17 09:02:25

您尝试过 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


好吧,你知道吗——“Self”技巧也有效......

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

Did you try ValueMember = "" or ValueMember = "."?

Really hacky, but you could add a property on ColorName that is itself? (perhaps via a partial class)

public ColorName Self {get {return this;}}

then set `ValueMember = "Self";'

Other than that, you'd probably need a TypeConverter

The other option might be to override ToString() on ColorName to return Name, and not have a value/display member?


(update: no it doesn't)

Have checked, and ToString() seems to work :

public override string ToString() { return Name; }

and just don't set a DisplayMember or a ValueMember.


Well whad'ya know - the "Self" trick works too ...

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文