基于组合框启用或禁用gridview内的文本框

时间:2018-07-07 12:24:25

标签: c# .net winforms

我的要求基于在同一datagrid内的组合框中选择的值启用或禁用gridview内的文本框。gridview内的组合框是否发生任何值更改事件?

1 个答案:

答案 0 :(得分:0)

win窗体工具箱中的gridview有一些事件,首先,您可以用来处理 CellValueChanged 。使用此事件,您可以从任何控件中获取当前选定的值,另一个方法是处理组合的SelectedIndexChanged

在网格视图中处理组合框选择更改所需的源代码有些不同,以下代码显示了该过程:

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];
    //HERE YOU SHOULD DISABLE OR ENABLE TEXTBOX
}