Вы можете изменить DataGridViewTextBoxCell
на DataGridViewComboBoxCell
в событии CellBeginEdit
и вернуть его обратно в событие CellEndEdit
.
Попробуйте это:
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{ DataGridViewComboBoxCell cb=new DataGridViewComboBoxCell();
cb.Items.Add(dataGridView1.CurrentCell.Value);
//add your other itmes here;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = cb;//change the DataGridViewTextBoxCell to DataGridViewComboBoxCell
}
delegate void settexthandler(DataGridViewCellEventArgs e); //use delegate to prevent reentrant call
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.BeginInvoke(new settexthandler(settoTextBox), new object[] { e });
}
void settoTextBox(DataGridViewCellEventArgs e)
{
DataGridViewTextBoxCell tb = new DataGridViewTextBoxCell();
tb.Value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = tb; //set it back to DataGridViewTextBoxCell with newly selected value
}