简体   繁体   中英

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. 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. The current value of the combobox will be equal to the selected value.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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