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

发布于 2024-09-28 02:21:04 字数 1389 浏览 2 评论 0原文

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

到目前为止我已经尝试过:

  • DataGridView.MultiSelect = true 允许
  • 在 DataGridView_CellMouseClick-Event 中选择多行和单元格 ClearSelection() 并重新选择最后选定的单元格看起来不太好(您会看到旧单元格取消选择,然后新的单元格选择;SuspendLayout() 和 ResumeLayout() 没有帮助)
  • DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect 不是一个选项:如果用户单击一个单元格,则应该仅选择该单元格

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

问候,

inno

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

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

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);
            }
        }
    }
}

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

HTH,谢谢和问候,

inno

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

What I've tried so far:

  • DataGridView.MultiSelect = true allows to select multi rows and cells
  • 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.SelectionMode = DataGridViewSelectionMode.FullRowSelect is not an option: If the user clicks a cell, it should only this cell get selected

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.

HTH, thanks and regards,

inno

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-10-05 02:21:04

您可以覆盖 SetSelectedRowCoreSetSelectedCelCore 并执行自定义选择。

MSDN 引用:

DataGridView控件使用这个
方法每当它改变
细胞的选择状态。这
选择状态发生变化而不考虑
到当前的 SelectionMode 属性
值,并且不改变
当前单元格属性值。这是
当您想要实施您的
自己的选择模式

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文