开发者

Confusion in DataGridView combobox

I'm currently using DataGridView with three cells, and the first cell is DataGridViewComboBoxColumn object, and I want to ensure wh开发者_如何学Pythonenever I select any new item in DataGridViewComboBoxColumn object other cells of dataGridview get empty. It doesn't matter if I reselect the same item again.

Could anyone please tell me how should I ensure that I've selected new item in DataGridViewComboBoxColumn object? Which property or method should I use for this approach?


You can declare a global List<int> gridComboSelections and when you bind your DataSource to your grid, you can fill this list with the SelectedValues of the comboboxes. When any of the combobox' value is changed, find the position of the combobox and check if it is same with gridComboSelections[i]. If it is same end operation, if not do what you want with it. If the value is changed, remember to change the corresponding value on your List.


You can refer following code which does same thing.

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                combo.SelectionChangeCommitted += new EventHandler(combo_SelectionChangeCommitted);
            }
        }

        void combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            DataGridViewComboBoxEditingControl combo = sender as DataGridViewComboBoxEditingControl;
            if (combo != null)
            {
                for (int columnIndex = 0; columnIndex < dataGridView1.ColumnCount; columnIndex++)
                {
                    if (columnIndex != combo.EditingControlDataGridView.CurrentCell.ColumnIndex)
                    {
                        dataGridView1[columnIndex, combo.EditingControlRowIndex].Value = null;
                    }
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜