禁用数据网格视图中的行选择

时间:2021-11-27 14:45:26

I want to disable the selection of certain rows in a datagridview.

我想禁用datagridview中某些行的选择。

It must be possible to remove the select property for one or more datagridview rows in a datagridview shown in a winform. The goal is that the user can't select certain rows. (depending on a condition)

必须可以删除winform中显示的datagridview中的一个或多个datagridview行的select属性。目标是用户无法选择某些行。 (视情况而定)

Thankx,

2 个解决方案

#1


16  

If SelectionMode is FullRowSelect, then you'll need to override SetSelectedRowCore for that DataGridView, and not call the base SetSelectedRowCore for rows you don't want selected.

如果SelectionMode是FullRowSelect,那么您将需要为该DataGridView覆盖SetSelectedRowCore,而不是为您不想选择的行调用基本SetSelectedRowCore。

If SelectionMode is not FullRowSelect, you'll want to additionally override SetSelectedCellCore (and not call the base SetSelectedCellCore for rows you don't want selected), as SetSelectedRowCore will only kick in if you click the row header and not an individual cell.

如果selectionMode是不是FullRowSelect,你要另外重写SetSelectedCellCore(而不是调用基SetSelectedCellCore你不想选择的行),因为如果你单击该行标题,而不是单个细胞SetSelectedRowCore只会一命呜呼

Here's an example:

这是一个例子:

public class MyDataGridView : DataGridView
{
    protected override void SetSelectedRowCore(int rowIndex, bool selected)
    {
        if (selected && WantRowSelection(rowIndex))
        {
            base.SetSelectedRowCore(rowIndex, selected);
        }
     }

     protected virtual void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
     {
         if (selected && WantRowSelection(rowIndex))
         {
            base.SetSelectedRowCore(rowIndex, selected);
          }
     }

     bool WantRowSelection(int rowIndex)
     {
        //return true if you want the row to be selectable, false otherwise
     }
}

If you're using WinForms, crack open your designer.cs for the relevant form, and change the declaration of your DataGridView instance to use this new class instead of DataGridView, and also replace the this.blahblahblah = new System.Windows.Forms.DataGridView() to point to the new class.

如果您正在使用WinForms,请打开您的designer.cs以获取相关表单,并更改DataGridView实例的声明以使用此新类而不是DataGridView,并替换this.blahblahblah = new System.Windows.Forms。 DataGridView()指向新类。

#2


-1  

Private Sub dgvSomeDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles dgvSomeDataGridView.SelectionChanged
        dgvSomeDataGridView.ClearSelection()
End Sub

#1


16  

If SelectionMode is FullRowSelect, then you'll need to override SetSelectedRowCore for that DataGridView, and not call the base SetSelectedRowCore for rows you don't want selected.

如果SelectionMode是FullRowSelect,那么您将需要为该DataGridView覆盖SetSelectedRowCore,而不是为您不想选择的行调用基本SetSelectedRowCore。

If SelectionMode is not FullRowSelect, you'll want to additionally override SetSelectedCellCore (and not call the base SetSelectedCellCore for rows you don't want selected), as SetSelectedRowCore will only kick in if you click the row header and not an individual cell.

如果selectionMode是不是FullRowSelect,你要另外重写SetSelectedCellCore(而不是调用基SetSelectedCellCore你不想选择的行),因为如果你单击该行标题,而不是单个细胞SetSelectedRowCore只会一命呜呼

Here's an example:

这是一个例子:

public class MyDataGridView : DataGridView
{
    protected override void SetSelectedRowCore(int rowIndex, bool selected)
    {
        if (selected && WantRowSelection(rowIndex))
        {
            base.SetSelectedRowCore(rowIndex, selected);
        }
     }

     protected virtual void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
     {
         if (selected && WantRowSelection(rowIndex))
         {
            base.SetSelectedRowCore(rowIndex, selected);
          }
     }

     bool WantRowSelection(int rowIndex)
     {
        //return true if you want the row to be selectable, false otherwise
     }
}

If you're using WinForms, crack open your designer.cs for the relevant form, and change the declaration of your DataGridView instance to use this new class instead of DataGridView, and also replace the this.blahblahblah = new System.Windows.Forms.DataGridView() to point to the new class.

如果您正在使用WinForms,请打开您的designer.cs以获取相关表单,并更改DataGridView实例的声明以使用此新类而不是DataGridView,并替换this.blahblahblah = new System.Windows.Forms。 DataGridView()指向新类。

#2


-1  

Private Sub dgvSomeDataGridView_SelectionChanged(sender As Object, e As System.EventArgs) Handles dgvSomeDataGridView.SelectionChanged
        dgvSomeDataGridView.ClearSelection()
End Sub