简体   繁体   English

当我单击 C# 中相同 winForm 中其他 datagridview 中的单元格时,在 datagridview 中的 Datagridviewcomboboxcell 中设置一个值

[英]Set a value in a Datagridviewcomboboxcell in datagridview when i click a cell in other datagridview in the same winForm in C#

I have two Datagridview in the same WinForm, in one I have a column that is of the Datagridviewcomboboxcell type with their assigned values from a list.我在同一个 WinForm 中有两个 Datagridview,在一个中我有一个 Datagridviewcomboboxcell 类型的列,它们从列表中分配值。 I would like to be able to set a current value in the combobox cell when I select a value in a cell of the second datagridview.当我 select 在第二个 datagridview 的单元格中设置值时,我希望能够在 combobox 单元格中设置当前值。 The current value of the combobox will be equal to the selected value. combobox 的当前值将等于所选值。

I would really appreciate the help.我非常感谢您的帮助。

I assume the rows are equal for both DataGridView controls, then you can set the cell value on the CellValueChanged event:我假设两个 DataGridView 控件的行相等,然后您可以在CellValueChanged事件上设置单元格值:

private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells["ColumnName"].Value = 
        dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}

There will be necessary to add some guards to avoid exceptions when eg index is out of range.当索引超出范围时,有必要添加一些保护以避免异常。

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

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