如何处理 ComboBox 的 SelectedIndexChanged 事件? [英] How to handle SelectedIndexChanged event for a ComboBox?

查看:31
本文介绍了如何处理 ComboBox 的 SelectedIndexChanged 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 DataGridView,其中包含两个 ComboBox 列.第二个 ComboBox 将根据从第一个 ComboBox 中选择的值填充数据.

I have DataGridView which contains two ComboBox columns. The second ComboBox will be filled with data depending on the selected value from first ComboBox.

如何处理第一个 ComboBoxSelectedIndexChanged 事件.

How to handle the SelectedIndexChanged event for the first ComboBox.

推荐答案

可以在此处找到有关 DataGridView 问题的重要资源:

A great resource for DataGridView questions can be found here:

http://www.windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

从那里开始介绍如何处理选定的更改事件:

From there on how to handle a selected change event:

如何处理 SelectedIndexChanged 事件?

How do I handle the SelectedIndexChanged event?

有时了解用户何时选择了 ComboBox 编辑控件中的项目会很有帮助.使用表单上的 ComboBox,您通常会处理 SelectedIndexChanged 事件.使用 DataGridViewComboBox,您可以通过使用 DataGridView.EditingControlShowing 事件来做同样的事情.下面的代码示例演示了如何执行此操作.请注意,该示例还演示了如何防止触发多个 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.

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");
}

这篇关于如何处理 ComboBox 的 SelectedIndexChanged 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆