4
votes

I am trying to implement a DataGridView that allows both selecting an entire row by clicking the row header or selecting an entire column by clicking the column header.

I've tried overriding the OnColumnHeaderMouseClick function and manually selecting the column. This only works if I set the SelectionMode to ColumnHeaderSelect before the event happens.

Any help on how to get this behavior would be greatly appreciated!

Here is the relevant code from my DataGridView:

public class CustomDataGridView : DataGridView
{
    protected override void OnRowHeaderMouseClick(DataGridViewCellMouseEventArgs e)
    {
        this.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
        base.OnRowHeaderMouseClick(e);
    }

    protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e)
    {
        this.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
        base.OnColumnHeaderMouseClick(e);
    }
}
2
Can you share your code? Are you using the winforms DataGridView?Ryan Gates
Hi Ryan, I'm using Winforms and I've edited my original post to contain the little code that I have (that's relevant to this question)amura.cxg

2 Answers

3
votes

I think you have to do this manually:

protected override void OnRowHeaderMouseClick(DataGridViewCellMouseEventArgs e) {
  this.ClearSelection();
  for (int i = 0; i < this.Columns.Count; ++i) {
    this.Rows[e.RowIndex].Cells[i].Selected = true;
  }
  base.OnRowHeaderMouseClick(e);
}

protected override void OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e) {
  this.ClearSelection();
  for (int i = 0; i < this.Rows.Count; ++i) {
    this.Rows[i].Cells[e.ColumnIndex].Selected = true;
  }
  base.OnColumnHeaderMouseClick(e);      
}

Make sure the SelectionMode = CellSelect

1
votes

Late to the party here, but there's no need to do this manually (i.e. loop through the entire dgv).

The OP was pretty close to what you need. Just capture the Header Click events for Columns and Rows and set the SelectionMode accordingly. Then forcibly select whichever Column/Row they clicked on.

This makes it function just like an Excel spreadsheet where you can either select Cells individually, or select entire Columns/Rows by clicking on the headers. You can also select multiple columns/rows by holding down Shift/Ctrl. It also allows for Shift-Space selection of the entire Column/Row (depending on which Mode you're in). This is similar to Excel, except Excel allows distinct functions: Shift-Space to select rows and Ctrl-Space to select columns, but hey... we can't have it all.

Just make sure your initial SelectionMode for the dgv is Column/RowHeaderSelect or CellSelect, not FullColumn/RowSelect otherwise their first clicks before they click on a Column/Row header won't function as desired.

Private Sub dgvView_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvView.ColumnHeaderMouseClick
    With dgvView
        If .SelectionMode <> DataGridViewSelectionMode.ColumnHeaderSelect Then
            .SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
            .Columns(e.ColumnIndex).Selected = True
        End If
    End With
End Sub
Private Sub dgvView_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvView.RowHeaderMouseClick
    With dgvView
        If .SelectionMode <> DataGridViewSelectionMode.RowHeaderSelect Then
            .SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
            .Rows(e.RowIndex).Selected = True
        End If
    End With
End Sub