欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-20 21:32:28
...

转载地址:https://*.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
}
}