简体   繁体   English

如何以编程方式更改 datagridview 中的组合框所选项目?

[英]How to programmatically change combobox selected item in datagridview?

如何以编程方式更改 datagridview 中的组合框所选项目?

Using brute force.使用蛮力。 Assuming you want to change the value of a DataGridViewComboBoxCell in the column colIndex and row rowIndex , and the value index of the combobox is in the 0 position:假设您要更改列colIndex和行rowIndex中的DataGridViewComboBoxCell的值,并且组合框的值索引在 0 位置:

For jj = 0 To CType(myDatagrid.Rows(rowIndex).Cells(colIndex), DataGridViewComboBoxCell).Items.Count - 1
    If CInt(CType(myDatagrid.Rows(colIndex).Cells(rowIndex), DataGridViewComboBoxCell).Items(jj).row(0)) = myValue Then
        myDatagrid.Rows(rowIndex).Cells(colIndex).Value = CType(myDatagrid.Rows(rowIndex).Cells(colIndex), DataGridViewComboBoxCell).Items(jj).row(0)
    End If
Next

Yes, I know this question was asked 6 years ago, but I haven't found the answer anywhere and I found it by myself.是的,我知道这个问题是 6 年前提出的,但我在任何地方都没有找到答案,我自己找到了。 Hope it will be useful to someone.希望它对某人有用。

The reason why DataGridViewComboBoxCell has not selectedIndex or selectedValue properties is beyond my grasp. DataGridViewComboBoxCell没有selectedIndexselectedValue属性的原因超出了我的理解。

In the DataGridView CellClick event:在 DataGridView CellClick 事件中:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
     string item = dataGridView1.SelectedCells[0].Value.ToString();
     comboBox1.SelectedItem = item;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM