我在 winforms 应用程序中有 DataGridViewComboBoxCell,我正在为每一行填充一些数据源。这就是我在这个线程DataGridViewComboBoxColumn 上找到的,向每一行添加不同的项目。 这工作正常,每一行都有不同的下拉值。但是当我在列标题上单击鼠标并进行排序时,所有 DataGriViewComboBoxCells 都被清除,并且无法选择任何值。我必须对列进行排序。不会发生异常。一些解决方案?
private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, object[] itemsToAdd)
{
DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell) dataGrid.Rows[rowIndex].Cells[colIndex];
// You might pass a boolean to determine whether to clear or not.
dgvcbc.Items.Clear();
foreach (object itemToAdd in itemsToAdd)
{
dgvcbc.Items.Add(itemToAdd);
}
}