点击特定行并点击button
后,我想将cell
类型从默认类型(DataGridViewTextBoxCell
)更改为DataGridViewComboBoxCell
并更改单元格数据源到列表。
不知怎的,我遇到了一个无法解释的行为,在一行中它起作用并显示ComboBox
,在另一行中,它只是进入编辑模式。
我的代码看起来像这样:
private void btnClick(object sender, EventArgs e)
{
dataGridView.BeginEdit(true);
var selectedRow = CurrentCell.RowIndex;
var selectedColumn = CurrentCell.ColumnIndex;
var cellName = dataGridView[0, _selectedCell.RowIndex].Value.ToString();
var dict = GetDict(cellName);
if (dict != null)
{
var comboBoxCell = new DataGridViewComboBoxCell
{
DataSource = dict.Keys.ToList()
}
dataGridView[1, selectedRow] = comboBoxCell;
dataGridView.CurrentCell = dataGridView.Rows[selectedRow].Cells[selectedColumn];
}
dataGridView.BeginEdit(false);
}
更新:似乎只有在event
CellBeginEdit
被触发后,这是由于按下其他cell
更新引起的,但是它需要像3 -4单击以ComboBox
打开。
答案 0 :(得分:1)
试用此代码。您应该覆盖CellClick
事件或在那里给您打电话。此代码以CellClick
事件编写,不要与名称
//This Code is written in CellClick Event not in CellContentClick (Don'tconfuse with signature)
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.BeginEdit(true);
var selectedRow = (sender as DataGridView).CurrentCell.RowIndex;
var selectedColumn = (sender as DataGridView).CurrentCell.ColumnIndex;
var comboBoxCell = new DataGridViewComboBoxCell
{
};
dataGridView1.CurrentCell = dataGridView1.CurrentCell as DataGridViewComboBoxCell;
dataGridView1[selectedColumn, selectedRow] = comboBoxCell;
dataGridView1.CurrentCell = dataGridView1.Rows[selectedRow].Cells[selectedColumn];
dataGridView1.BeginEdit(false);
}