C# 如何在 DataGridViewComboBoxCell 中选择一个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11657345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select a value in a DataGridViewComboBoxCell?
提问by stepler
I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set ValueMember, and DisplayMember.
我有 DataGridViewComboBoxCell 和一个 DataTable。表 I 中的数据使用 DataSource 与 DataGridViewComboBoxCell 绑定并设置 ValueMember 和 DisplayMember。
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
dataGridView1.Rows[0].Cells[0] = comboBoxCell;
comboBoxCell.DataSource = dataTable;
comboBoxCell.ValueMember = "ID";
comboBoxCell.DisplayMember = "Item";
}
How can I programmatically set the value in the cell when the form loads? In the simple ComboBox I know a property SelectedIndex. I tried comboBoxCell.Value = ...; but it gives an exception. And tried
加载表单时,如何以编程方式设置单元格中的值?在简单的 ComboBox 中,我知道一个属性 SelectedIndex。我试过 comboBoxCell.Value = ...; 但它给出了一个例外。并尝试
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
e.Value = 1;
}
It sets a new value in the cell, but I need to select a value.
它在单元格中设置了一个新值,但我需要选择一个值。
Form loaded and I have empty cell.
表单已加载,我有空单元格。


And some data in the ComboBox.
以及 ComboBox 中的一些数据。


When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1";right after comboBoxCell.DisplayMember = ... (see above), it works fine.
当我将此代码放在dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1";comboBoxCell.DisplayMember = ... (见上文)之后时,它工作正常。
The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.
ID 列中的值“1”对应于 Items 列中的值“Second”。所以,我得到了正确的结果。


Sorry for my English and my newbie code :)
对不起我的英语和我的新手代码:)
采纳答案by David Hall
Instead of adding a cell to your grid add a DataGridViewComboBoxcolumn.
不是在网格中添加单元格,而是添加一DataGridViewComboBox列。
DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn();
c.Name = "ComboColumn";
c.DataSource = dataTable;
c.ValueMember = "ID";
c.DisplayMember = "Item";
dataGridView1.Columns.Add(c);
To select a particular value you set the Value property of a given cell.
要选择特定值,请设置给定单元格的 Value 属性。
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1;
Note that the type here is important! In comments you say you get a
System.FormatException. This can be caused by setting the wrong type to the value.When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the
System.FormatExceptionexception you are seeing.If the types differ you need to either update the DataTable definition or set the value to a string:
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";- Also note that this value must be presentin the ID column of the DataTable that you have set as the source of the grid.
注意这里的类型很重要!在评论中你说你得到了一个
System.FormatException. 这可能是由于将错误的类型设置为值造成的。当您将该值设置为 1 时,您正在分配一个 int - 如果由于某种原因您在 ID 列中有字符串,您将
System.FormatException看到您看到的异常。如果类型不同,您需要更新 DataTable 定义或将值设置为字符串:
dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1";- 另请注意,此值必须出现在您设置为网格源的 DataTable 的 ID 列中。
It is usually easiest to work with a DataGridViewwhen it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid's DataSource using the DataPropertyName property.
DataGridView当a设置了 DataSource 时,它通常最容易使用。在这种情况下,您可以使用 DataPropertyName 属性将 ComboBoxColumn 绑定到网格的数据源。
c.DataPropertyName = "GridDataSourceColumnName";
This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.
这允许从网格数据源中获取列值,并允许对列的更改直接更改该数据源。
Lastly, do not use the CellFormatting event here - this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.
最后,不要在此处使用 CellFormatting 事件 - 此事件不适用于此类用途。通常最好在 DataBindingComplete 事件中(如果您只想完成一次)或在需要 DefaultValues 或 RowValidating 等某些事件期间执行此类工作。
By using CellFormatting you will probably make it impossible for users to manually edit the combo box.
通过使用 CellFormatting,您可能无法让用户手动编辑组合框。

