DataGridView override top,left header cell click (select all)

转载地址:https://stackoverflow.com/questions/1504620/datagridview-override-top-left-header-cell-click-select-all

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?

c#
winforms
datagridview
header
selectall
Share
Edit
Follow
edited Jan 11 '11 at 15:45

Bernhard Hofmann
9,8151212 gold badges5959 silver badges7878 bronze badges
asked Oct 1 '09 at 15:25
Greg Kendall
Add a comment
4 Answers

5

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:

Disable multi selection (if your business logic permits)
Inherit your own datagrid and override OnCellMouseDown (something like this)

protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == -1) return;
base.OnCellMouseDown(e);
}
Share
Edit
Follow
edited Nov 23 '11 at 12:49

Jason Plank
2,32244 gold badges2929 silver badges3939 bronze badges
answered Oct 28 '09 at 19:04

anchandra
1,0791010 silver badges2121 bronze badges
Thanks for the response. I need full row select. The other is intriging but, unfortunately, the gridview still selected all the columns when I tried this before. I ended up unselecting the rows as a work around but not a very pretty one. – Greg Kendall Nov 17 '09 at 14:53
So you did the override and the selection still happened? I did a quick test myself and it seemed to work. – anchandra Nov 23 '09 at 15:45
Add a comment

1

I know this is late but hopefully it will help someone. The code below worked for me in a similar scenario.

private void MyDataGridView_MouseUp(object sender, MouseEventArgs e)
{
    DataGridView.HitTestInfo hitInfo = this.MyDataGridView.HitTest(e.X, e.Y);
    if (hitInfo.Type == DataGridViewHitTestType.TopLeftHeader)
    {
        MyDataGridView.ClearSelection();
    }
 }

Share
Edit
Follow
answered Sep 1 '15 at 16:16

Emily
1122 bronze badges
Add a comment

0

override OnCellMouseDown method:

if (e.ColumnIndex == -1 && e.RowIndex == -1)
{
return;
}
else
{
base.OnCellMouseDown(e);
}
Share
Edit
Follow
answered Nov 28 '20 at 11:55

anefeletos
59566 silver badges1717 bronze badges
Add a comment

0

You could gain some control over the click event using this hack 😃

private void dataGridView1_Click(object sender, EventArgs e)
{
MouseEventArgs args = (MouseEventArgs)e;
DataGridView dgv = (DataGridView)sender;
DataGridView.HitTestInfo hit = dgv.HitTest(args.X, args.Y);
if (hit.Type == DataGridViewHitTestType.TopLeftHeader)
{
// do something here
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值