Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# DataGridView cell dirty state event not fired when combobox dropdown opened by Enter, but item selected with Mouse

$
0
0

Hi! I think I found a bug in DataGridView. When I select item in DataGridView Combobox only by mouse, then dirtystate event fires 2 times. First time when drop down is opened, and second time when I select an item from Combobox dropdown. I have overriden ProcessCmdKey so I can now open Combobox dropdown list by hitting Enter on selected cell, then selecting item with arrow keys and hitting Enter again - selects an item in Combobox. This works great. Using Enter "dirty state" event also is fired two times.

The problem (possible BUG) is when I open Combobox dropdown list by hitting Enter, and then select the item by Mouse left click. In this case dirty state event does not fire.

I wonder, why this happens? Could anyone from experienced C# gurus explain why dirty state event does not fire up?

I add a simple example for demonstration:

public sealed class CustomDataGridView : DataGridView
{
    public CustomDataGridView()
    {
        AllowDrop = false;
        AllowUserToResizeColumns = false;
        AllowUserToResizeRows = false;
        RightToLeft = RightToLeft.No;
        EnableHeadersVisualStyles = false;
        ScrollBars = ScrollBars.Vertical;

        CurrentCellDirtyStateChanged += OnCurrentCellDirtyStateChanged;
    }

    private void OnCurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        var dataGridView = sender as DataGridView;
        if (dataGridView != null)
        {
            if (dataGridView.CurrentRow != null && !dataGridView.CurrentRow.IsNewRow)
            {
                if (dataGridView.CurrentCell != null)
                {
                    if (dataGridView.CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
                    {
                        if (dataGridView.IsCurrentCellDirty)
                        {
                            var inputValue = ((string)dataGridView.CurrentCell.EditedFormattedValue);
                            MessageBox.Show("Selected value is: " + inputValue);
                        }
                    }
                }
            }
        }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            if (CurrentCell != null)
            {
                if (CurrentCell.GetType() == typeof(DataGridViewComboBoxCell))
                {
                    if (CurrentCell.IsInEditMode)
                    {
                        var comboBox = EditingControl as DataGridViewComboBoxEditingControl;
                        if (comboBox != null)
                        {
                            //If cell is edited and combobox is shown, then on Enter press - select current item
                            if (comboBox.DroppedDown)
                            {
                                var inputValue = comboBox.GetItemText(comboBox.SelectedItem);
                                comboBox.DroppedDown = false;
                                comboBox.SelectedIndex = comboBox.Items.IndexOf(inputValue);
                                EndEdit();
                                NotifyCurrentCellDirty(true);
                                return true;
                            }
                        }
                    }
                    else
                    {
                        //If cell is selected, then on Enter press - open combobox drop down
                        BeginEdit(false);
                        var comboBox = EditingControl as DataGridViewComboBoxEditingControl;
                        if (comboBox != null)
                        {
                            comboBox.SelectedIndex = 0;
                            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                            comboBox.Focus();
                            comboBox.DroppedDown = true;
                        }
                        return true;
                    }
                }

            }
        }
        //Process all other keys as expected
        return base.ProcessCmdKey(ref msg, keyData);
    }

}


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>