DataGridView select RowHeader or ColumnHeader

本文关键字:ColumnHeader or RowHeader select DataGridView | 更新日期: 2023-09-27 18:05:54

我正在尝试实现一个DataGridView,允许通过单击行标头来选择整个行或通过单击列标头来选择整个列。

我已经尝试重写OnColumnHeaderMouseClick函数并手动选择列。这只适用于如果我设置的SelectionMode ColumnHeaderSelect事件发生之前。

任何关于如何获得此行为的帮助将非常感激!

下面是我的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);
    }
}

DataGridView select RowHeader or ColumnHeader

我认为你必须手动完成:

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);      
}

确保SelectionMode = CellSelect

来晚了,但不需要手动执行此操作(即遍历整个dgv)。

OP非常接近你所需要的。只需捕获列和行的Header Click事件,并相应地设置SelectionMode。然后强制选择他们单击的列/行。

这使得它的功能就像一个Excel电子表格,你可以单独选择单元格,或者通过点击标题选择整个列/行。您也可以通过按住Shift/Ctrl选择多个列/行。它还允许整个列/行的Shift-Space选择(取决于您所处的模式)。这与Excel类似,除了Excel允许不同的功能:Shift-Space选择行,Ctrl-Space选择列,但是嘿……我们不可能拥有一切。

只是确保你的初始选择模式的dgv是列/RowHeaderSelect或CellSelect,而不是FullColumn/RowSelect,否则他们的第一次点击之前,他们点击一个列/行标题将不会按预期的功能。

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。净:

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