在 DataGridViewComboBoxColumn SelectedIndexChanged 期间触发的事件 [英] Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

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

问题描述

我有带有两列的 DataGridView.第一列是 TextBoxCol(DataGridViewTextBoxColumn),第二列是 ComboBoxCol(DataGridViewComboBoxColumn).

I have DataGridView with two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn) and the Second one is ComboBoxCol(DataGridViewComboBoxColumn).

ComboBoxCol 更改其选定索引值时,如何更改TextBoxCol 的值?(这应该发生在 ComboBoxCol 中选定的索引更改时.不是在离开列之后,例如 dataGridView_CellValueChanged)

How can I change the value of TextBoxCol when ComboBoxCol changes its selected index value? (This should happen when selected index changed in ComboBoxCol. Not after leaving the column, like dataGridView_CellValueChanged)

我读过一个主题,问题几乎与我遇到的问题相同,但我不明白答案(根据复选标记应该是正确的).这是链接.-几乎相同的主题

I have read one topic with almost the same problem that I am having but I dont understand the answer(which should be correct base on the check mark). Here's the link. -Almost same topic

推荐答案

试一下这两个简单的方法(top方法中的'1'是combobox列的索引)

Give these two simple methods a go (the '1' in the top method is the index of the combobox column)

您要修改的行将是 setter 行 cel.Value =,因为您可以将其更改为您喜欢的任何内容.

The line that you would make you modifications to would be the setter line cel.Value =, as you may change it to whatever you like.

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged;
            comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
        }
    }

    private void LastColumnComboSelectionChanged(object sender, EventArgs e)
    {
        var currentcell = dataGridView1.CurrentCellAddress;
        var sendingCB = sender as DataGridViewComboBoxEditingControl;
        DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
        cel.Value = sendingCB.EditingControlFormattedValue.ToString();
    }


这篇关于在 DataGridViewComboBoxColumn SelectedIndexChanged 期间触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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