DataGridView:如何启用多行选择但禁用多单元格选择?

时间:2021-08-18 14:45:03

I'm looking for a way to enable multi row select in a DataGridView-Control but disable multi cell select.

我正在寻找一种在DataGridView-Control中启用多行选择但禁用多单元格选择的方法。

What I've tried so far:

到目前为止我尝试了什么:

  • DataGridView.MultiSelect = true allows to select multi rows and cells
  • DataGridView.MultiSelect = true允许选择多行和单元格

  • ClearSelection() in DataGridView_CellMouseClick-Event and re-select the last selected cell doesn't look very nice (you see the old cell deselect and then the new cell select; SuspendLayout() and ResumeLayout() doesn't help)
  • DataGridView_CellMouseClick-Event中的ClearSelection()并重新选择最后选择的单元格看起来不太好(您看到旧单元格取消选择然后新单元格选择; SuspendLayout()和ResumeLayout()没有帮助)

  • DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect is not an option: If the user clicks a cell, it should only this cell get selected
  • DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect不是一个选项:如果用户单击一个单元格,它应该只选中此单元格

It's for an export function: The user should be able to export selected rows to a file but in general he should not be able to select more than one cell (for copy & paste etc.).

它用于导出功能:用户应该能够将选定的行导出到文件中,但通常他不能选择多个单元格(用于复制和粘贴等)。

Regards,

inno

----- [UPDATE] -----

----- [更新] -----

Here's my implementation. Works fine (comments removed for compactness):

这是我的实施。工作正常(为紧凑性删除注释):

using System.Windows.Forms;

namespace YourAmazingNamespace
{
    public partial class SpecialSelectDataGridView: DataGridView
    {
        public SpecialSelectDataGridView()
        {
            InitializeComponent();
        }

        protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
        {
            ResetSelectedCells();

            base.SetSelectedCellCore(columnIndex, rowIndex, selected);
        }

        void ResetSelectedCells()
        {
            foreach (DataGridViewCell cell in SelectedCells) {
                base.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
            }
        }
    }
}

Multiple rows are selected through MultiSelect = true (default value) and currently selected cells are resetted by calling ResetSelectedCells() before selecting the new one.

通过MultiSelect = true(默认值)选择多行,并在选择新的单元之前通过调用ResetSelectedCells()重置当前选定的单元格。

HTH, thanks and regards,

HTH,谢谢和问候,

inno

1 个解决方案

#1


1  

You could override SetSelectedRowCore or SetSelectedCelCore and perform your custom selection.

您可以覆盖SetSelectedRowCore或SetSelectedCelCore并执行自定义选择。

MSDN Quote:

The DataGridView control uses this method whenever it changes the selection state of a cell. The selection state changes without regard to the current SelectionMode property value, and without changing the CurrentCell property value. This is useful when you want to implement your own selection modes

只要DataGridView控件更改单元格的选择状态,它就会使用此方法。选择状态更改时不考虑当前的SelectionMode属性值,也不更改CurrentCell属性值。当您想要实现自己的选择模式时,这非常有用

Of course this means you will have to use an derived datagrid and not the standard one.

当然,这意味着您必须使用派生的数据网格而不是标准数据网格。

#1


1  

You could override SetSelectedRowCore or SetSelectedCelCore and perform your custom selection.

您可以覆盖SetSelectedRowCore或SetSelectedCelCore并执行自定义选择。

MSDN Quote:

The DataGridView control uses this method whenever it changes the selection state of a cell. The selection state changes without regard to the current SelectionMode property value, and without changing the CurrentCell property value. This is useful when you want to implement your own selection modes

只要DataGridView控件更改单元格的选择状态,它就会使用此方法。选择状态更改时不考虑当前的SelectionMode属性值,也不更改CurrentCell属性值。当您想要实现自己的选择模式时,这非常有用

Of course this means you will have to use an derived datagrid and not the standard one.

当然,这意味着您必须使用派生的数据网格而不是标准数据网格。