2

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

4

1 回答 1

4

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

您要修改的行将是 setter line 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();
    }

在此处输入图像描述

于 2012-06-30T21:15:46.640 回答