none
如何使DataGridView某些行不能被选中? RRS feed

  • 问题

  • 前面问了一个问题,但是与想要的有些偏差,所以重新发了这个问题。
    如何使DataGridView某些行不能被选中呢?
    原本设想在RowLeave事件中记录当前离开的行(先判断不是限制进入的行),在RowEnter事件中判断如果是限制进入的行,则设置CurrentCell为此前离开的那一行。可是这个方法不行:(

    Code Snippet

    Public leaveRow As Integer = 0

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
    If e.RowIndex = 2 Then
    DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, leaveRow)

    End If
    End Sub

    Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
    If e.RowIndex <> 2 Then
    leaveRow = e.RowIndex
    End If
    End Sub



    2008年4月20日 6:41

答案

  • Code Snippet

    Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
            Dim hit As DataGridView.HitTestInfo
            hit = DataGridView1.HitTest(e.X, e.Y)
            enterRow = hit.RowIndex
        End Sub


        Private Sub DataGridView1_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
            If (leaveRow <> 2 And enterRow = 2) Then
                e.Cancel = True
            End If
        End Sub

     

     

    2008年4月20日 12:06
    版主
  • 谢谢。我现在使用另一种方法实现了。请大家帮我参谋一下是否有致命的漏洞。

    Code Snippet

    Private disableRow As New Dictionary(Of Integer, Integer)  
    Public Sub AddDisableRow(ByVal rowIndex As Integer)
     
    If Not disableRow.ContainsKey(rowIndex) Then
     
    disableRow.Add(rowIndex, rowIndex)
    End If

    End Sub

    Public Sub RemoveDisableRow(ByVal rowIndex As Integer)
     
    If Not disableRow.ContainsKey(rowIndex) Then
     
    disableRow.Remove(rowIndex)
    End If

    End Sub

    Public Sub ClearDisableRow(ByVal rowIndex As Integer)
     
    disableRow.Clear()
    End Sub

    Protected Overrides Function SetCurrentCellAddressCore(ByVal columnIndex As Integer, ByVal rowIndex As Integer, ByVal setAnchorCellAddress As Boolean, ByVal validateCurrentCell As Boolean, ByVal throughMouseClick As Boolean) As Boolean
     
    If Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect AndAlso disableRow.Count > 0 AndAlso disableRow.ContainsKey(rowIndex) Then
     
    Me.SetSelectedCellCore(columnIndex, rowIndex, False)
     
    Me.SetSelectedRowCore(rowIndex, False)
     
    Me.SetSelectedRowCore(Me.CurrentRow.Index, True)
    Return False
    End If
    Return MyBase.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick)
    End Function


    2008年4月21日 4:46

全部回复

  • Code Snippet

    Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
            Dim hit As DataGridView.HitTestInfo
            hit = DataGridView1.HitTest(e.X, e.Y)
            enterRow = hit.RowIndex
        End Sub


        Private Sub DataGridView1_RowValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
            If (leaveRow <> 2 And enterRow = 2) Then
                e.Cancel = True
            End If
        End Sub

     

     

    2008年4月20日 12:06
    版主
  • 谢谢。我现在使用另一种方法实现了。请大家帮我参谋一下是否有致命的漏洞。

    Code Snippet

    Private disableRow As New Dictionary(Of Integer, Integer)  
    Public Sub AddDisableRow(ByVal rowIndex As Integer)
     
    If Not disableRow.ContainsKey(rowIndex) Then
     
    disableRow.Add(rowIndex, rowIndex)
    End If

    End Sub

    Public Sub RemoveDisableRow(ByVal rowIndex As Integer)
     
    If Not disableRow.ContainsKey(rowIndex) Then
     
    disableRow.Remove(rowIndex)
    End If

    End Sub

    Public Sub ClearDisableRow(ByVal rowIndex As Integer)
     
    disableRow.Clear()
    End Sub

    Protected Overrides Function SetCurrentCellAddressCore(ByVal columnIndex As Integer, ByVal rowIndex As Integer, ByVal setAnchorCellAddress As Boolean, ByVal validateCurrentCell As Boolean, ByVal throughMouseClick As Boolean) As Boolean
     
    If Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect AndAlso disableRow.Count > 0 AndAlso disableRow.ContainsKey(rowIndex) Then
     
    Me.SetSelectedCellCore(columnIndex, rowIndex, False)
     
    Me.SetSelectedRowCore(rowIndex, False)
     
    Me.SetSelectedRowCore(Me.CurrentRow.Index, True)
    Return False
    End If
    Return MyBase.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick)
    End Function


    2008年4月21日 4:46