Cant find method DataGridView.ClearSelection(Int32, Int32, Boolean)

STL 21 Reputation points
2020-11-29T13:26:44.327+00:00

Hello, I am using VS VB.Net with .NET Framework 4.8.
I want to clear the selection of all cells in a row of a DataGridView except for one cell.
The .NET API Browser shows a method for that "ClearSelection(Int32, Int32, Boolean)".
But with my DataGridView that method is not available. I can only use the normal "ClearSelection()".
How can i use "ClearSelection(Int32, Int32, Boolean)"?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,118 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-11-29T14:26:26.807+00:00

    Hi,
    you cannot find that method because is protected.

    protected void ClearSelection (int columnIndexException, int rowIndexException, bool selectExceptionElement);

    If you really need that method than you can extend DataGridView...

    Try following demo:

    Public Class Form1  
      
      Private WithEvents dgv As New ExtendedDataGridView With {.Dock = DockStyle.Fill}  
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Me.KeyPreview = True  
        Me.Controls.Add(dgv)  
        Dim col As New List(Of Data)  
        For i = 1 To 10  
          col.Add(New Data With {.ID = i, .Field1 = $"Row {i} Col 1", .Field2 = $"Row {i} Col 1"})  
        Next  
        dgv.DataSource = col  
      End Sub  
      
      Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown  
        If e.KeyCode = Keys.C Then  
          dgv.ClearSelectionEx(1, 5, True)  
          e.Handled = True  
        End If  
      End Sub  
      
      Public Class Data  
        Public Property ID As Integer  
        Public Property Field1 As String  
        Public Property Field2 As String  
      End Class  
      
      Public Class ExtendedDataGridView : Inherits DataGridView  
      
        Public Sub ClearSelectionEx(columnIndex As Integer, rowIndex As Integer, selectElement As Boolean)  
          MyBase.ClearSelection(columnIndex, rowIndex, selectElement)  
        End Sub  
      
      End Class  
      
    End Class  
    

    Result:

    43425-x.gif


0 additional answers

Sort by: Most helpful