1

I have implemented a DataGridViewComboBox that allows editing using the code from this thread.

My problem right now is that when I focus the cell, the value is cleared. Based on the posts I've read, the item must be added to the combobox first. So here's what I have tried. Can you possibly tell me what's going wrong?

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
        {
            DataGridViewRow row = dataGridView1.CurrentRow;
            DataGridViewCell cell = dataGridView1.CurrentCell;

            if (cell == row.Cells[colComboBox.Name])
            {
                DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
                control.DropDownStyle = ComboBoxStyle.DropDown;

                //For testing purposes
                colComboBox.Items.Add("Test");//I'm adding the item to the combobox control
                row.Cells[colComboBox.Name].Value = "Test";//Then set the value of the cell based on the item I added
            }
        }
    }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == colComboBox.Index)
        {
            colComboBox.Items.Clear();
            object eFV = e.FormattedValue;
            if (!colComboBox.Items.Contains(eFV))
            {
                colComboBox.Items.Add(eFV);
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
            }
        }
    }
Community
  • 1
  • 1
jmc
  • 1,649
  • 6
  • 26
  • 47
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 07 '15 at 13:50
  • Upvoted for good title – Matthew Frontino Apr 07 '15 at 13:53

2 Answers2

0

In the example you linked, the Validating event used is the editing combo box one and not the DataGridView's CellValidating one.

By the way, using the EditingControlShowing event to access the editing control is generally a bad idea. I would suggest you to use a cell template by adding this code before populating your datagridview :

DataGridViewComboBoxCell templateCell = new DataGridViewComboBoxCell();
templateCell.Items.Add("Test");
templateCell.Items.Add("Test2");
colComboBox.CellTemplate = templateCell;
Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • Hi, thanks for pointing out that I'm using the DataGridView's event. More or less I get the same result, just that other cells are affected too. Anyway, I think using the EditingControlShowing event is necessary as that's where I have to transform the combobox's style. Specifically, the effect I really want is that of a textbox control, where when you focus the textbox, the contents are highlighted and not cleared. – jmc Apr 08 '15 at 05:24
0

I'm not entirely sure why the DataGridViewComboBox behaves this way. But experimenting on to the ComboBox control with default properties, it does not clear the value when focused and behaves just like a TextBox control. The solution is just to actually set the Text property of the control to a e.g. global variable of some sort that can be accessed by other events bound to the control. As for now this is the behaviour that I'm trying to achieve.

DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
        control.DropDownStyle = ComboBoxStyle.DropDown;
        control.Text = SomeGlobalVariable;
jmc
  • 1,649
  • 6
  • 26
  • 47