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

查看:541
本文介绍了如何处理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.

如何处理第一个 ComboBox SelectedIndexChanged 事件。 / p>

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天全站免登陆