动态更改datagridview中组合框列中的值 [英] dynamically change values in combo box columns in datagridview

查看:70
本文介绍了动态更改datagridview中组合框列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,
当我在第一个组合框列中更改值时,我在datagridview中有3个组合框列
然后第二个组合框列应填充相应的值,然后通过更改第二个组合框值填充第三个组合框列的相应值.我该怎么做?

例如:
第一组合框列:类别
显示成员:A,B,C
第二个组合框列:产品
显示成员:a,a1,a2 b,b1,b2 c,c1,c2

当我在combobox1中选择``A''时,则第二个组合框应只包含\ display a,a1,a2而不显示其他
当我在combobox1中选择``B''时,则第二个组合框应只包含\ display b,b1,b2而不显示其他

如果有人有解决方案,请回答


i have 3 combobox columns in datagridview,when i change value in 1st combobox column
then 2nd combobox column should fill with corresponding values,and by changing 2nd combobox value then 3rd combobox column should fill with corresponding values.how can i do this?

Ex:
first combo box column: Category
display members: A,B,C
second combo box column:Products
display members:a,a1,a2 b,b1,b2 c,c1,c2

when i select ''A'' in combobox1 ,then 2nd combobox should contain\display a,a1,a2 only not others
and when i select ''B'' in combobox1 ,then 2nd combobox should contain\display b,b1,b2 only not others

Plese answer if anyone have solution

推荐答案

我认为只需要一些EventHander,就可以查询ComboBox的api.
I think it just need some EventHander,you can query the api of ComboBox.


如果要在更改combobox1中的值后立即(即,甚至在离开combobox1之前)更新combobox2,则需要处理combobox1上的SelectedIndexChanged事件.在该事件的处理程序中,您可以修改combobox2的下拉菜单项和当前值.

为SelectedIndexChanged设置处理程序需要一些工作.您必须处理DataGridView.EditingControlShowing事件并添加类似以下的代码:

If you want combobox2 to update as soon as the value in combobox1 has been changed, i.e. before you even leave combobox1, you will need to handle the SelectedIndexChanged event on combobox1. In the handler for that event you can modify combobox2''s dropdown items and current value.

Setting up a handler for SelectedIndexChanged needs a little work. You must handle the DataGridView.EditingControlShowing event and add code something like this:

protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
{
    base.OnEditingControlShowing(e);

    if ((e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
    {
        // Always remove handler before adding
        ((ComboBox)e.Control).SelectedIndexchanged -= new EventHandler(myCombo_OnSelectedIndexChanged);
        ((ComboBox)e.Control).SelectedIndexchanged += new EventHandler(myCombo_OnSelectedIndexChanged);
    }
}

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    // Update combobox2 items
}



显然,您将需要修改以上代码以确保捕获所需的组合框,但这是您需要的机制.



You will obviously need to modify the above code to ensure that you trap the required comboboxes, but this is the mechanism you need.


这篇关于动态更改datagridview中组合框列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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