简体   繁体   English

DataGridView 选择 RowHeader 或 ColumnHeader

[英]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.我正在尝试实现一个 DataGridView,它允许通过单击行标题选择整行或通过单击列标题选择整列。

I've tried overriding the OnColumnHeaderMouseClick function and manually selecting the column.我尝试覆盖 OnColumnHeaderMouseClick 函数并手动选择列。 This only works if I set the SelectionMode to ColumnHeaderSelect before the event happens.这只适用于在事件发生之前将 SelectionMode 设置为 ColumnHeaderSelect 的情况。

Any help on how to get this behavior would be greatly appreciated!任何有关如何获得此行为的帮助将不胜感激!

Here is the relevant code from my DataGridView:这是我的 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);
    }
}

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确保SelectionMode = CellSelect

Late to the party here, but there's no need to do this manually (ie loop through the entire dgv).迟到了,但没有必要手动执行此操作(即遍历整个 dgv)。

The OP was pretty close to what you need. OP 非常接近您的需要。 Just capture the Header Click events for Columns and Rows and set the SelectionMode accordingly.只需捕获列和行的标题单击事件并相应地设置 SelectionMode。 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.这使它的功能就像 Excel 电子表格一样,您可以在其中单独选择单元格,也可以通过单击标题来选择整个列/行。 You can also select multiple columns/rows by holding down Shift/Ctrl.您还可以通过按住 Shift/Ctrl 来选择多个列/行。 It also allows for Shift-Space selection of the entire Column/Row (depending on which Mode you're in).它还允许对整个列/行进行 Shift-Space 选择(取决于您所在的模式)。 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.这类似于 Excel,除了 Excel 允许不同的功能:Shift-Space 选择行和 Ctrl-Space 选择列,但是嘿......我们不能拥有它。

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.只需确保 dgv 的初始 SelectionMode 是 Column/RowHeaderSelect 或 CellSelect,而不是 FullColumn/RowSelect 否则他们在单击列/行标题之前的第一次单击将无法正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM