DataGridView 覆盖顶部,左侧标题单元格单击(全选) [英] DataGridView override top,left header cell click (select all)

查看:28
本文介绍了DataGridView 覆盖顶部,左侧标题单元格单击(全选)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖在 DataGridView 标题/列单元格(顶部、左侧单元格)中单击鼠标的行为.该单元格会导致选择所有行.相反,我想阻止它选择所有行.我看到 RowHeaderSelect 和 ColumnHeaderSelect 的事件,但没有看到顶部左侧标题单元格的事件.

I want to override the behavior of a mouse click in the DataGridView header/column cell (top, left cell). That cell causes all rows to be selected. Instead, I want to stop it from selecting all rows. I see an event for RowHeaderSelect and ColumnHeaderSelect but not one for that top, left header cell.

有什么想法吗?我只是瞎了吗?

Any ideas? Am I just being blind?

推荐答案

这是当你点击那个单元格时发生的反汇编代码:

This is the dissasembled code of what happens when you click that cell:

private void OnTopLeftHeaderMouseDown()
{
    if (this.MultiSelect)
    {
        this.SelectAll();
        if (-1 != this.ptCurrentCell.X)
        {
            this.SetCurrentCellAddressCore(this.ptCurrentCell.X, this.ptCurrentCell.Y, false, false, false);
        }
    }

为了防止这种行为,您有两种解决方案:

In order for you to prevent this behavior you have 2 solutions:

  1. 禁用多选(如果您的业务逻辑允许)
  2. 继承你自己的数据网格并覆盖OnCellMouseDown(类似这样)

protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex == -1 && e.ColumnIndex == -1) return;
    base.OnCellMouseDown(e);
}

这篇关于DataGridView 覆盖顶部,左侧标题单元格单击(全选)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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