DataGridViewComboBoxColumn-必须单击两次单元格才能显示组合框 [英] DataGridViewComboBoxColumn - Have to click cell twice to display combo box

查看:548
本文介绍了DataGridViewComboBoxColumn-必须单击两次单元格才能显示组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 DataGridView ,它是使用设计器创建的,其中包含几列,包括 DataGridViewComboBoxColumn 列。

I am using a DataGridView, created using the designer with a few columns including a DataGridViewComboBoxColumn column.

有点恼人的是,我必须在每个单元格上单击两次甚至三次才能显示下拉列表:

It is slightly irritating that I have to click on each cell twice or even three times to display the drop-list:


  1. 如果我单击文本部分,则需要 3 次点击!

  1. If I click over the text part, it takes 3 clicks!




  1. 如果我单击向下箭头,则只有两次单击:


我认为这是由于单元格使用第一次单击来获得焦点,但是有没有办法解决该问题,因此单击单元格会立即显示组合框?我注意到,使用 DataGridViewCheckBoxColumn 不会发生相同的问题……单击复选框会立即切换它,无论该单元格是否具有焦点。

I assume it's due to the cell using the first click to get focus, but is there a way to address the issue so clicking on a cell displays the combobox right away? I note that using DataGridViewCheckBoxColumn the same issue does not happen... clicking on a checkbox immediately toggles it regardless if that cell had focus.

推荐答案

您可以简单地设置 EditMode DataGridView EditOnEnter 的属性。

You can simply set EditMode property of your DataGridView to EditOnEnter.

它使编辑更加容易。几乎是单击,但是即使您立即单击 ComboBoxColumn 的组合框显示下拉菜单的内容,也可以处理 CellClick 事件,然后使用网格的 EditingControl 并将其转换为 DataGridViewComboBoxEditingControl 并使其显示下拉菜单。

It makes editing more easy. Almost single click, but if you want even when you click on content of combobox show dropdown for your ComboBoxColumn immediately, you can handle CellClick event and then use EditingControl of your grid and cast it to DataGridViewComboBoxEditingControl and makes it to show dropdown.

private void categoryDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //You can check for e.ColumnIndex to limit this to your specific column
    var editingControl = this.categoryDataGridView.EditingControl as 
        DataGridViewComboBoxEditingControl;
    if (editingControl != null)
        editingControl.DroppedDown = true;
}

使用此技巧时要小心,可能会使下拉列表令人讨厌当用户只希望在单元格之间导航而不进行编辑时。

这篇关于DataGridViewComboBoxColumn-必须单击两次单元格才能显示组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆