以编程方式设置 DataGridView 中组合框的选定索引 [英] Programmatically set selected index of a ComboBox in DataGridView
本文介绍了以编程方式设置 DataGridView 中组合框的选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在避免数据绑定的同时,在 DataGridView 的 ComboBox 中设置选定的索引.它没有连接到数据库.
I want to, while avoiding databinding, to set the selected index in a ComboBox in a DataGridView. It is not connected to a DB.
我发现的所有解决方案都将 DataGridView 连接到数据库,但我没有,所以我无法解决问题.
All solutions I have found have the DataGridView connected to a Database, which I have not, so I couldn't solve the problem.
推荐答案
由于 DataGridViewComboBoxColumn 没有 SelectedIndex 或 SelectedValue 属性,您可以尝试像这样设置值:
Since the DataGridViewComboBoxColumn has no SelectedIndex or SelectedValue properties, you can try and set the value like this example:
DataGridViewComboBoxColumn cmbCurrencies = (DataGridViewComboBoxColumn)myDataGridView.Columns["ComboboxCurrencyColumn"];
var currencies = entities.currencies.Select(c => c.currencyName).DefaultIfEmpty().ToList();
cmbCurrencies.DataSource = currencies;
然后:
for (int i = 0; i <= myDataGridView.RowCount - 1; i++)
{
myDataGridView.Rows[i].Cells["Index of Combobox Column"].Value = "Pound";
}
另见如果 这个 可能会有所帮助.
See also if this may help.
这篇关于以编程方式设置 DataGridView 中组合框的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文