当DatagridviewComboBox选择索引更改时

当DatagridviewComboBox选择索引更改时

本文介绍了当DatagridviewComboBox选择索引更改时,更改DatagridviewTextBox文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 DatagridView 中,我有两个列, ComboxboxColumn TextboxColumn 。当组合框选择索引更改时,我想更改文本框
的值(一般来说,组合框已经选择了索引更改事件,但 datagridviewComboBox 没有)

In My DatagridView,I Have Two Columns, ComboxboxColumn and TextboxColumn. I want to change the value of textboxwhen combobox selected index changes (in general combobox it has selected index change event but datagridviewComboBox does not have it)

推荐答案

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

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;
        }
    }

    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();
    }







这篇关于当DatagridviewComboBox选择索引更改时,更改DatagridviewTextBox文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 11:22