DataGridViewComboBoxColumn SelectedIndexChanged中触发的事件 [英] Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

查看:538
本文介绍了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

推荐答案

给出这两个简单的方法(顶部的'1'是combobox列的索引)

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

您将使您修改的行将是设置行 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;
        }
    }

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