当DatagridviewComboBox选择索引更改时更改DatagridviewTextBox文本

时间:2012-06-30 21:00:52

标签: c# .net winforms datagridview datagridcomboboxcolumn

在我的DatagridView中,我有两列,ComboxboxColumnTextboxColumn。我想更改文本框的值 当组合框选择了索引更改时(通常组合框中它选择了索引更改事件,但datagridviewComboBox没有它)

1 个答案:

答案 0 :(得分:4)

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

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


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

enter image description here