DataGridComboBoxColumn shows first value on CellEnter

妖精的绣舞 提交于 2019-12-12 03:04:49

问题


I am having weird results in my DataGridViewComboBoxColumn.

1) When there is no value set, and I click on the cell (not the drop down arrow) the first value in the options shows as the value, and when I click off the cell, it returns back to blank value. This is misleading as there was no value chosen, yet it shows the first value until you click off the cell.

One solution was to make the first option blank, however then I am still afflicted by the next issue...

2) If I select a cell that already has a value, then select a cell that does not, the value that was originally selected will show in the cell that has no value selected. Again, clicking off the cell will clear it, however, it is misleading again.

I would like to make this not misleading ... anyone know of a solution?

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); 

DataTable dt = new DataTable();
dt.Columns.Add("f_Id");
dt.Columns[0].DataType = typeof(string);
dt.Columns.Add("f_Desc");
dt.Columns[1].DataType = typeof(string);

for (int i = 0; i < values.Count(); i++)
{
    dt.Rows.Add(values[i], values[i]);
}

col.DataSource = dt;
col.DisplayMember = dt.Columns[1].ColumnName;
col.ValueMember = dt.Columns[0].ColumnName;
col.HeaderText = header;
col.Name = header;

DataGridView1.Columns.Add(col);

回答1:


This has been plaguing me for a while as well and Zeeshanef's code helped but still not not totally solve it for me. Upon further investigation, I found if you use the DefaultValuesNeeded method to set some default values, it's possible the cell value might be set but the combobox Control that gets displayed in the EditingControlShowing method has an empty value/text. I solved it by tweaking the code like this:

private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)                
        {
            ComboBox comboBox = (ComboBox)e.Control;
            if (datagridview1.CurrentCell.Value == null
                || string.IsNullOrEmpty(datagridview1.CurrentCell.Value.ToString())
                || string.IsNullOrEmpty(comboBox.SelectedText)
                )
            {
                comboBox.SelectedIndex = -1;
            }               
        }
    }



回答2:


I was also searching for this and finally solved this problem. create following event of DatagridView and set [comboboxcolumnNo] as your datagridcomboboxcolumn number.

Now whenever combobox column will get focus to edit, then it will check if current cell value is null, if it is null then it will display -1 index of combobox which shows nothing.

    private void datagridview1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (datagridview1.CurrentCell.ColumnIndex == comboboxcolumnNo && e.Control is ComboBox)
        {
            ComboBox comboBox = (ComboBox)e.Control;
            if (datagridview1.CurrentCell.Value == null)
            {
                comboBox.SelectedIndex = -1;
            }
        }
    }


来源:https://stackoverflow.com/questions/16153681/datagridcomboboxcolumn-shows-first-value-on-cellenter

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