Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. Disable multi selection (if your business logic permits)
  2. 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);
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...