DataGridView Default Error

本文介绍在使用DataGridViewComboBoxColumn时可能出现的错误,原因在于添加新行时未指定ValueMember。提供了通过代码设置DataSource、DisplayMember和ValueMember的解决方案,以及订阅DataGridView的DataError事件来处理错误的方法。

The following error may be raised when creating a new row that contains a DataGridViewComboBox

The cause of this error is that when adding a new row with default value, its ValueMember is not specified.

In order to solve this problem, you can use the following code:

public struct IndexField
{
    public IndexField(int id, string name)
   {
        ID = id;
        Name = name;
    }
    public int ID { get; private set; }
    public string Name { get; private set; }
}

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

    private void Form1_Load(object sender, EventArgs e)
    {
        IndexField[] indexFields = new IndexField[]
        {
              new IndexField(1, "p1"),
              new IndexField(2, "p2"),
              new IndexField(3, "p3")
        };
        Populate(indexFields);
    }

    public void Populate(IndexField[] indexFields)
    {
        DataGridViewComboBoxColumn indexFieldColumn = new DataGridViewComboBoxColumn()
        {
            HeaderText = "Index Fields"
        };

        indexFieldColumn.DataSource = indexFields;
        indexFieldColumn.DisplayMember = nameof(IndexField.Name);
        indexFieldColumn.ValueMember = nameof(IndexField.ID);
        dataGridView1.Columns.Add(indexFieldColumn);

        for (int i = 0; i < 5; i++)
        {
            dataGridView1.Rows.Add(indexFields[0].ID, indexFields[0].Name);
            // Will cause error:dataGridView1.Rows.Add(indexFields[0].Name);
        }
        dataGridView1.AllowUserToAddRows = false;
    }
}
View Code

In addition, you can solve this problem in another way. You can subscribe the DataError event of the DataGridView when the form is initialized.

The sample code for this solution is as follows:

public Form1()
{
    InitializeComponent();
    dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
}
View Code

It will also go wrong if you create a new line in the following way.

for (int i = 0; i < 5; i++)
{
    DataGridViewRow row = new DataGridViewRow();
    DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
    comboxcell.DataSource = indexFields;
    comboxcell.DisplayMember = "Name";
    comboxcell.ValueMember = "ID";
    row.Cells.Add(comboxcell);
    dataGridView1.Rows.Add(row);
    dataGridView1.Rows[i].Cells[0].Value = "p1";
}
dataGridView1.AllowUserToAddRows = false;
View Code

In this case, you can also subscribe the DataError event of the DataGridView when the form is initialized.

public Form1()
{
    InitializeComponent();
    dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
}
View Code

Caution: When setting the DataGridViewComboBoxCell.Value, note that the value set must be the value contained in the DataGridViewComboBoxCell.Item collection, otherwise an error occurs after the setting is completed.

转载于:https://www.cnblogs.com/jizhiqiliao/p/9830422.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值