Change DatagridviewTextBox Text When DatagridviewComboBox Selected Index Changes

感情迁移 提交于 2019-12-12 18:33:55

问题


In My DatagridView,I Have Two Columns, ComboxboxColumn and TextboxColumn. I want to change the value of textbox when 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)

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



来源:https://stackoverflow.com/questions/11277632/change-datagridviewtextbox-text-when-datagridviewcombobox-selected-index-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!