C# 如何处理 ComboBox 的 SelectedIndexChanged 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11950189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle SelectedIndexChanged event for a ComboBox?
提问by IBRA
I have DataGridViewwhich contains two ComboBoxcolumns.
The second ComboBoxwill be filled with data depending on the selected value from first ComboBox.
我有DataGridView其中包含两ComboBox列。第二个ComboBox将根据从 first 中选择的值填充数据ComboBox。
How to handle the SelectedIndexChangedevent for the first ComboBox.
如何处理第一个SelectedIndexChanged事件ComboBox。
采纳答案by UWSkeletor
A great resource for DataGridView questions can be found here:
可以在此处找到有关 DataGridView 问题的重要资源:
http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc
http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc
From there on how to handle a selected change event:
从那里开始如何处理选定的更改事件:
How do I handle the SelectedIndexChanged event?
如何处理 SelectedIndexChanged 事件?
Sometimes it is helpful to know when a user has selected an item in the ComboBox editing control. With a ComboBox on your form you would normally handle the SelectedIndexChanged event. With the DataGridViewComboBox you can do the same thing by using the DataGridView.EditingControlShowing event. The following code example demonstrates how to do this. Note that the sample also demonstrates how to keep multiple SelectedIndexChanged events from firing.
有时,了解用户何时选择了 ComboBox 编辑控件中的项目会很有帮助。使用表单上的 ComboBox,您通常会处理 SelectedIndexChanged 事件。使用 DataGridViewComboBox,您可以通过使用 DataGridView.EditingControlShowing 事件来做同样的事情。下面的代码示例演示了如何执行此操作。请注意,该示例还演示了如何防止触发多个 SelectedIndexChanged 事件。
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= new
EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectedIndexChanged += new
EventHandler(cb_SelectedIndexChanged);
}
}
void cb_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected index changed");
}
回答by Krishna Thota
Place the second dropdownlist in Update panel like this
像这样将第二个下拉列表放在更新面板中
<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="ddl2" runat="server" >
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
set AutoPostBack="true" property for first dropdownlist like this.
像这样为第一个下拉列表设置 AutoPostBack="true" 属性。
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
回答by algreat
If I use EditingControlShowingevent then cb_SelectedIndexChangedfires several times, even when user selects combobox but doesn't change selection.
如果我使用EditingControlShowing事件然后cb_SelectedIndexChanged触发几次,即使用户选择组合框但不更改选择。
This works for me:
这对我有用:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == comboboxColumn.Index && e.RowIndex >= 0) //check if combobox column
{
object selectedValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
}
}
//changes must be committed as soon as the user changes the drop down box
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

