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

How to programmatically change combobox selected item in datagridview?
2021-05-11
  •  译文(汉语)
  •  原文(英语)

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

速聊1:
只需将单元格值更改为comboboxcolumn的一项,它将自动为您选择.
速聊2:
所以给我代码示例作为答案:)我们都知道理论部分:)
速聊3:
以太写答案或什么都不写.在这里编写我的代码的0逻辑.
解决过程1

使用蛮力.假设您要更改DataGridViewComboBoxCellcolumncolIndex和row中的arowIndex的值,并且组合框的值索引位于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

是的,我知道这个问题是6年前提出的,但是我在任何地方都找不到答案,而是我自己找到的.希望对某人有用.

为什么原因DataGridViewComboBoxCell还没有selectedIndexselectedValue性能超出了我的把握.

解决过程2

在DataGridView CellClick事件中:

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

How to programmatically change combobox selected item in datagridview?

Talk1:
Just change the cell value to one of items of the comboboxcolumn, it will auto select for you.
Talk2:
so give me code example in answer :) we all know theoretical part :)
Talk3:
Ether write answer or don't write nothing. 0 logic in writing my code here.
Solutions1

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:

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. Hope it will be useful to someone.

The reason why DataGridViewComboBoxCell has not selectedIndex or selectedValue properties is beyond my grasp.

Solutions2

In the DataGridView CellClick event:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
     string item = dataGridView1.SelectedCells[0].Value.ToString();
     comboBox1.SelectedItem = item;
}
转载于:https://stackoverflow.com/questions/20217595/how-to-programmatically-change-combobox-selected-item-in-datagridview

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入