具有ComboBox事件的DataGridView [已解决] [英] DataGridView with ComboBox events [Solved]

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

问题描述

问好质量检查答卷者,请您自己提出一个问题.

我先解释一下我在做什么.我有一个旧的DataGridView控件,必须保持这种类型.它包含3列,其中2列是ComboBox.当第一列更改为类型时,第二列的数据源应填充2个值.如果除此类型以外的任何内容,都应擦除数据集,以便无法访问它(一种禁用组合框的方式).

当此更改第一列组合框的选定索引时,我得到以下事件:

私有 void  dbPlatform_EditingControlShowing( object  _sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView dgv = _sender as DataGridView ;   

    ComboBox cb = e.Control as ComboBox ;   
    如果(cb!= null)
    {
        int row = dgv.CurrentCell.RowIndex ;   
        int column = dgv.CurrentCell.ColumnIndex ;   

        如果(列==  0 )
        {
            cb.SelectedIndexChanged-=(sender,args)=> cb_SelectedIndexChanged(发件人,行,列);   
            cb.SelectedIndexChanged + =(sender,args)=> cb_SelectedIndexChanged(发件人,行,列);   
        }
    }
} 



我真的只需要为此触发包含更改索引的组合框的行.问题是,假设我在第4行第1列进行了更改,每行都会调用一次.

添加:
我应该澄清大问题.我找不到一种方法来触发该事件的组合框单元格的行号或行索引.我可以将_sender转换为ComboBox或DataGridView,但不能转换为包含所需数据(行索引)的DataGridViewComboBoxCell. DataGridViewComboBoxEditingControl.我没有意识到这个对象,但是它似乎有我需要的row属性.


Greetings fellow QA answerers, time for a question from one of your own.

I will explain what I''m doing first. I have a DataGridView control that is legacy, has to remain this type. It contains 3 columns, 2 of which is a ComboBox. When the first column changes to a type, the datasource for the second column should be populated with 2 values. When it is anything except this type, the dataset should be wiped so it cannot be accessed (kind of a way to disable the combobox).

I get the event for when the selected index of the first column''s combo box is changed with this:

private void dbPlatform_EditingControlShowing(object _sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView dgv = _sender as DataGridView;

    ComboBox cb = e.Control as ComboBox;
    if (cb != null)
    {
        int row = dgv.CurrentCell.RowIndex;
        int column = dgv.CurrentCell.ColumnIndex;

        if (column == 0)
        {
            cb.SelectedIndexChanged -= (sender, args) => cb_SelectedIndexChanged(sender, row, column);
            cb.SelectedIndexChanged += (sender, args) => cb_SelectedIndexChanged(sender, row, column);
        }
    }
}



I really only need this to fire for the row that contains the combobox thats index is being changed. The problem is, lets say I have row 4 column 1 change, this gets called for each row.

ADDED:
I should clarify the big issue. I can''t find a way to get the row number or row index of the combobox cell that fires this event. I can cast the _sender as a ComboBox, or a DataGridView, but not a DataGridViewComboBoxCell which would contain the data I need (row index).

解决方案

I casted sender as DataGridViewComboBoxEditingControl. I was unaware of this object, but it seems to have a row property that I needed.


这篇关于具有ComboBox事件的DataGridView [已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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