我有两列DataGridView
。第一列是TextBoxCol(DataGridViewTextBoxColumn)
,第二列是ComboBoxCol(DataGridViewComboBoxColumn)
。
当TextBoxCol
更改其选定的索引值时,如何更改ComboBoxCol
的值?
(当ComboBoxCol
中的选定索引发生变化时,就会发生这种情况。离开列后不会发生,如dataGridView_CellValueChanged
)
我已经阅读了一个与我遇到的问题几乎相同的主题,但我不明白答案(根据复选标记应该是正确的)。这是链接。 -Almost same topic
答案 0 :(得分:39)
这个答案在几个地方浮动。使用DataGridViewEditingControlShowingEventHandler将触发比您想要的更多事件。在我的测试中,它多次触发了该事件。另外使用combo.SelectedIndexChanged - =事件不会真正删除事件,它们只是保持堆叠。无论如何,我找到了一个似乎有效的解决方案。我在下面提供了一个代码示例:
// Add the events to listen for
dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
// This fires the cell value changed handler below
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// My combobox column is the second one so I hard coded a 1, flavor to taste
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1];
if (cb.Value != null)
{
// do stuff
dataGridView1.Invalidate();
}
}
答案 1 :(得分:29)
给出这两个简单的方法(top方法中的'1'是组合框列的索引)
您可以修改的行是设置行cel.Value =
,因为您可以将其更改为您喜欢的任何内容。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged;
comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
}
}
private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
var currentcell = dataGridView1.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
cel.Value = sendingCB.EditingControlFormattedValue.ToString();
}
答案 2 :(得分:2)
该链接是正确的。处理DataGridView的EditingControlShowing event
。在此事件处理程序中,检查当前列是否符合您的兴趣。然后,创建一个临时组合框对象: -
ComboBox comboBox = e.Control as ComboBox;
MSDN有一个示例:请参阅示例部分here。
请注意 Inheritance Hierarchy
& msdn链接中的Class Syntax
: -
公共类DataGridViewComboBoxEditingControl: ComboBox , IDataGridViewEditingControl
private DataGridView dataGridView1 = new DataGridView();
private void AddColorColumn()
{
DataGridViewComboBoxColumn comboBoxColumn =
new DataGridViewComboBoxColumn();
comboBoxColumn.Items.AddRange(
Color.Red, Color.Yellow, Color.Green, Color.Blue);
comboBoxColumn.ValueType = typeof(Color);
dataGridView1.Columns.Add(comboBoxColumn);
dataGridView1.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(
dataGridView1_EditingControlShowing);
}
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}