开发者

.NET Windows Forms datagridview - have to click combobox column three times to get focus

I'm making a Windows Forms application which has a standard DataGridView in it. The DataGridView has several DataGridViewComboBoxColumns in it. And they are a pain to work with. To get one o开发者_如何学Cf them to open up (as in, drop down the list), you have to click the cell at least 3 (!!!) times. Seems that the first click ends the editing of the previous cell; The second click activates the cell; and only the third click gets sent to the combobox itself. User interface nightmare, especially when you have to enter a lot of data through these comboboxes.

Is there any workaround?

Added: I just tried it in a blank solution. Just a single form with a single datagridview. The same issue. My colleagues are having the same problem, so I can't be the only one with this. Is there no standard workaround?


I ran into the same issue with a DataGridView of mine. I solved the problem by changing the selected cell before the DataGridView handles the click itself:

// Subscribe to DataGridView.MouseDown when convenient
this.dataGridView.MouseDown += this.HandleDataGridViewMouseDown;

private void HandleDataGridViewMouseDown(object sender, MouseEventArgs e)
{
    // See where the click is occurring
    DataGridView.HitTestInfo info = this.dataGridView.HitTest(e.X, e.Y);

    if (info.Type == DataGridViewHitTestType.Cell)
    {
        switch (info.ColumnIndex)
        {
            // Add and remove case statements as necessary depending on
            // which columns have ComboBoxes in them.

            case 1: // Column index 1
            case 2: // Column index 2
                this.dataGridView.CurrentCell =
                    this.dataGridView.Rows[info.RowIndex].Cells[info.ColumnIndex];
                break;
            default:
                break;
        }
    }
}


Here's a really easy way if you're okay with how it affects the other non-combo box controls: Set the DataGridView EditMode Property to EditOnEnter.

From http://social.msdn.microsoft.com/Forums/en/winforms/thread/bc454d5c-a1cb-4abb-939c-f16eb025d4cc


I had the same trouble and found/came up with this solution. It allows for first-time focusing on comboboxes and text boxes while allowing the user to select the row header to delete an item.

someDGV.EditMode = DataGridViewEditMode.EditOnEnter;  
someDGV.CellClick += new DataGridViewCellEventHandler(DGV_CellClick);

private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv = ((DataGridView)sender);
        if (e.ColumnIndex < 0)
        {
            dgv.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
            dgv.Focus();
            dgv.EndEdit(); 
        }
        else
        {
            dgv.EditMode = DataGridViewEditMode.EditOnEnter;
            dgv.BeginEdit(false);
        }
    }


i've had the same problem in one of my own projects.

i'm not sure of all the ramifications of this but i handled this issue by sending F4 when the combobox is entered. It works, but i'm hoping it doesnt blow something up down the line.

Ex:

'This call opens up the ComboBox dropdown immediately instead of having to click into it three times to open the stupid thing
SendKeys.Send("{F4}")

i feel your frustration as you can see from the comment ;)


Actually neither examples work for me. Sending "F4" is bad scenario because SendKeys.Send(...) has unstable behavior (MSDN). Also you cannot use WinForm window with SendKeys in WPF project.

Zach Johnson has good example, however for proper working there should be some modifications:

// Subscribe to DataGridView.MouseDown when convenient
this.dataGridView.MouseDown += this.HandleDataGridViewMouseDown;

private void HandleDataGridViewMouseDown(object sender, MouseEventArgs e)
{
    // See where the click is occurring
    DataGridView.HitTestInfo info = this.dataGridView.HitTest(e.X, e.Y);

    if (info.Type == DataGridViewHitTestType.Cell)
    {
        switch (info.ColumnIndex)
        {
            // Add and remove case statements as necessary depending on
            // which columns have ComboBoxes in them.

            case 1: // Column index 1
            case 2: // Column index 2
                this.dataGridView.CurrentCell =
                    this.dataGridView.Rows[info.RowIndex].Cells[info.ColumnIndex];

                //Begin edit current cell
                this.dataGridView.BeginEdit(true);
                //Find cell control, if it is combobox then drop it down 
                DataGridViewComboBoxEditingControl comboBoxCellControl = 
                this.dataGridView.EditingControl as DataGridViewComboBoxEditingControl;
                    if (comboBoxCellControl != null)
                    {
                        comboBoxCellControl.DroppedDown = true;
                    }
                break;
            default:
                break;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜