Moo*_*ght 4 c# combobox datagridview event-handling winforms
我在处理驻留在dataGridView中的comboBox的索引更改事件时遇到问题.我编写了一个方法来使用委托来处理comboBox选择更改:
ComboBox.SelectedIndexChanged -= delegate { ComboBoxIndexChanged(); };
ComboBox.SelectedIndexChanged += delegate { ComboBoxIndexChanged(); };
Run Code Online (Sandbox Code Playgroud)
或者一个EventHandler:
comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);
Run Code Online (Sandbox Code Playgroud)
但这两种方法都没有按预期工作.也就是说,当您在comboBox中单击您的选择(包含在dataGridView中)时,需要多次单击才能生成我的ComboBoxIndexChanged(); 正常运行的方法,如果它决定完全起作用.克服/关于在dataGridView中的comboBox的indexedChange上指定事件的最佳方法是什么?
我目前在上下文中使用的代码如下:
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
if (this.dataGridView.CurrentCell.ColumnIndex == (int)Column.Col)
{
ComboBox comboBox = e.Control as ComboBox;
if (comboBox != null)
{
comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);
}
}
return;
}
catch (Exception Ex)
{
Utils.ErrMsg(Ex.Message);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
并且事件ComboBoxIndexChanged是:
private void ComboBoxIndexChanged(object sender, EventArgs e)
{
// Do some amazing stuff...
}
Run Code Online (Sandbox Code Playgroud)
我已经在StackOverFlow上读了一个类似的线程,该线程表明以这种方式处理comboBox更改事件存在问题,但我无法使解决方案起作用.可以在此处找到帖子:Datagridview上的ComboBoxColumn中的"SelectedIndexChanged"事件.它说:
"事情变得复杂,因为他们通过对所有行只有一个编辑控件来优化DataGridView.这就是我处理类似情况的方式:
首先将委托连接到EditControlShowing事件:
myGrid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(
Grid_EditingControlShowing);
...
Run Code Online (Sandbox Code Playgroud)
然后在处理程序中,连接到EditControl的SelectedValueChanged事件:
void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// the event to handle combo changes
EventHandler comboDelegate = new EventHandler(
(cbSender, args) =>
{
DoSomeStuff();
});
// register the event with the editing control
combo.SelectedValueChanged += comboDelegate;
// since we don't want to add this event multiple times, when the
// editing control is hidden, we must remove the handler we added.
EventHandler visibilityDelegate = null;
visibilityDelegate = new EventHandler(
(visSender, args) =>
{
// remove the handlers when the editing control is
// no longer visible.
if ((visSender as Control).Visible == false)
{
combo.SelectedValueChanged -= comboDelegate;
visSender.VisibleChanged -= visibilityDelegate;
}
});
(sender as DataGridView).EditingControl.VisibleChanged +=
visibilityDelegate;
}
}"
Run Code Online (Sandbox Code Playgroud)
我对此的这个问题是没有定义"VisSender",因此无法使用"VisibleChanged"事件.
你小伙子的任何帮助,一如既往,最受赞赏.
SwD*_*n81 13
听起来您希望在用户更改下拉框后立即提交更改,而无需单击单元格.为了做到这一点,您需要在发生更改时强制提交(使用CommitEdit,MSDN页面上也有一个示例).将此添加到您的DataGridView:
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以只听,CellValueChanged并避免必须尝试在底层编辑控件上注册ComboBoxValueChanged事件.
| 归档时间: |
|
| 查看次数: |
11248 次 |
| 最近记录: |