C# dataGridView组合框事件处理程序问题
对于驻留在dataGridView中的组合框,我在处理索引更改事件时遇到问题。我编写了一个方法,使用委托处理组合框选择更改:C# dataGridView组合框事件处理程序问题,c#,winforms,datagridview,combobox,event-handling,C#,Winforms,Datagridview,Combobox,Event Handling,对于驻留在dataGridView中的组合框,我在处理索引更改事件时遇到问题。我编写了一个方法,使用委托处理组合框选择更改: ComboBox.SelectedIndexChanged -= delegate { ComboBoxIndexChanged(); }; ComboBox.SelectedIndexChanged += delegate { ComboBoxIndexChanged(); }; 或事件处理程序: comboBox.SelectedIndexChanged += ne
ComboBox.SelectedIndexChanged -= delegate { ComboBoxIndexChanged(); };
ComboBox.SelectedIndexChanged += delegate { ComboBoxIndexChanged(); };
或事件处理程序:
comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);
但这两种方法都没有达到预期效果。也就是说,当您在comboBox(包含在dataGridView中)中单击您的选择时,需要多次单击才能导致my ComboBoxIndexChanged();方法正确运行,如果它决定运行。克服/着手在dataGridView中组合框的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;
}
}
ComboBoxIndexChanged事件为:
private void ComboBoxIndexChanged(object sender, EventArgs e)
{
// Do some amazing stuff...
}
我读过一篇关于StackOverFlow的类似文章,其中指出以这种方式处理comboBox更改事件存在问题,但我无法找到有效的解决方案。可以在此处找到帖子:。它说:
“事情变得复杂了,因为他们优化了DataGridView,只对所有行使用一个编辑控件。下面是我如何处理类似情况的:
首先将委托连接到EditControlShowing事件:
myGrid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(
Grid_EditingControlShowing);
...
然后在处理程序中,连接到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;
}
}"
我对此的问题是“VisSender”没有定义,因此不能使用事件“VisibleChanged”
非常感谢您的帮助。听起来您希望在用户更改下拉框时立即提交更改,而不必单击单元格。为此,您需要在更改发生时强制提交(使用,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);
}
}
然后,您可以只侦听,而不必尝试在基础编辑控件上注册ComboBoxValueChanged事件。为什么要在代码中辅助事件处理程序?选择是否动态创建?是。这取决于comboBox的值,会影响dataGridView中的其他项。您可以发布一些相关的事件处理程序吗标记?我可能误解了您的意思。我单击dataGridView(DGV)中的comboBox列,它会下拉,我选择一个新索引-然后使用该索引(通过事件处理程序)设置其他内容。DGV有许多行(用户可以在运行时添加这些行),因此我需要一个EventHandler来处理许多可能的相应更改。谢谢。如果在标记中声明了下拉列表,您应该在那里分配事件处理程序。您使用的技术仅适用于动态创建的GridView。我已经为此困扰了数小时,这正是我想要的!非常感谢感谢您的帮助。如果您正在使用cascade combobox,那么这是一种终极方式