设置DisplayMember时,DataGridViewComboBoxColumn会发生什么?

时间:2013-11-08 14:27:21

标签: c# .net winforms datagridview datagridviewcombobox

如果我的DataGridViewComboBoxColumn填充了绑定值,并且我设置了DisplayMember属性,则会使用FormatException获取DataError事件:

  

DataGridViewComboBoxCell值无效

如果未设置DisplayMember,并且视图显示.ToString()的结果,则所有工作都按预期进行。

这是一个完整的例子:

public partial class Form1 : Form
{
    public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        var categories = new[] { CustomerCategory.Cat1, CustomerCategory.Cat2, CustomerCategory.Cat3 };
        this.dataGridView1.AutoGenerateColumns = false;
        this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
        this.dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
        this.dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
        {
            DataSource = categories,
            HeaderText = "Category",
            DataPropertyName = "Category",
            DisplayMember = "Name" // if we omit this line, there is not DataError event raised
        });

        this.dataGridView1.DataSource = new[] 
        { 
              new Customer() { Category = CustomerCategory.Cat1 } 
            , new Customer() { Category = CustomerCategory.Cat2 } 
            , new Customer() { Category = CustomerCategory.Cat3 } 
        }.ToList();
    }

    void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        var value = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        var type = value != null ? value.GetType() : null;
        string message = "Error"
            + Environment.NewLine + " - Column : " + e.ColumnIndex
            + Environment.NewLine + " - Line  : " + e.RowIndex
            + Environment.NewLine + " - Value : " + Convert.ToString(value) + " (" + type + ")"
            + Environment.NewLine + " - Exception : " + e.Exception.Message;
        Debug.Fail(message);
    }

    void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
    {
        //http://stackoverflow.com/questions/631126/how-to-bound-a-datagridviewcomboboxcolumn-to-a-object
        if (this.dataGridView1.CurrentCell.OwningColumn is DataGridViewComboBoxColumn)
        {
            var editingControl = (DataGridViewComboBoxEditingControl)this.dataGridView1.EditingControl;
            e.Value = editingControl.SelectedItem;
            e.ParsingApplied = true;
        }
    }
}

模特:

public class CustomerCategory
{
    public static readonly CustomerCategory Cat1 = new CustomerCategory { Name = "Cat1" };
    public static readonly CustomerCategory Cat2 = new CustomerCategory { Name = "Cat2" };
    public static readonly CustomerCategory Cat3 = new CustomerCategory { Name = "Cat3" };

    public string Name { get; set; }
    public override string ToString() { return this.Name; }
}
public class Customer { public CustomerCategory Category { get; set; } }

如果没有引发恼人的DisplayMember事件,如何指定我自己的DataError? 问题仅出现在DataGridViewComboBoxColumn,而不是常规ComboBox

编辑:经过几次测试后,我可以这样说:

[DisplayMember + Not ValueMember] = FAIL
[Not DisplayMember + ValueMember] = FAIL
[DisplayMember + ValueMember] = WIN

所以我的问题可以改写为:是否有任何文件可以准确地解释什么是有效的,什么是不可行的;以及DisplayMember + ValueMember如何像它似乎一样被链接在一起?

重新编辑:

一个有趣的参考:Problems with the DataGridViewComboBoxColumn

  

但是,DataGridViewComboBoxColumn不能像这样工作,   虽然如果不设置,它会显示ToString值   DisplayMember,当它试图查看时内部出错   在SelectedItem中,您必须将DisplayMember设置为public   你班上的财产。更糟糕的是,如果不这样做,则为默认行为   设置ValueMember属性是为了返回DisplayMember   无法获得实际物品本身。唯一的解决方法是添加一个   返回自身并将该属性设置为的类的属性   ValueMember。当然,如果您的物品不是您能够的物品   要改变(比如其中一个框架类)你必须要修改   一起容纳对象的容器对象。

是否有人有关于 内部出错 部分内容的任何信息?

1 个答案:

答案 0 :(得分:1)

以下逻辑可以帮助您解决问题。

我认为问题是代码行的顺序。在分配显示成员属性后分配数据源可能会导致错误。

更改此行;

this.dataGridView1.Columns.Add(new DataGridViewComboBoxColumn()
{
    DataSource = categories,
    HeaderText = "Category",
    DataPropertyName = "Category",
    DisplayMember = "Category" // if we omit this line, there is not DataError event raised
});

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.HeaderText = "Category";
col.DataSource = categories;
col.DisplayMember = "Category";
col.DataPropertyName = "Category";
this.dataGridView1.Columns.Add(col);

必须在DisplayMember和ValueMember之前分配数据源。