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:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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;
}
}
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:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public Form1()
{
InitializeComponent();
dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
}
It will also go wrong if you create a new line in the following way.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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;
In this case, you can also subscribe the DataError event of the DataGridView when the form is initialized.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public Form1()
{
InitializeComponent();
dataGridView1.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { };
}
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.