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

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

问题描述

DataGridView的包含两个组合框列。
中的第二个组合框将充满根据从第一组合框选定值数据。

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

如何办理的第一个组合框的的SelectedIndexChanged 事件

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编辑控制已经选择了一个项目就知道了。有了您的窗体上的组合框,你通常会处理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");
}

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

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