'DataGridView select RowHeader or ColumnHeader
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);
}
}
Solution 1:[1]
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.
C#:
private void dgvView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
{
if (dgvView.SelectionMode != DataGridViewSelectionMode.ColumnHeaderSelect)
{
dgvView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
dgvView.Columns(e.ColumnIndex).Selected = true;
}
}
}
private void dgvView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
{
if (dgvView.SelectionMode != DataGridViewSelectionMode.RowHeaderSelect)
{
dgvView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
dgvView.Rows(e.RowIndex).Selected = true;
}
}
}
VB.NET:
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
