2013-08-01 97 views
0

我使用文本框和DGV生成自動暗示搜索。一切工作正常,除此之外,我想要2個按鈕,提供功能去1行上下。你必須知道,在我搜索時,我將那些不包含搜索字符串的行設置爲.visible = falseDataGridView選擇行錯誤

從理論上講它應該像這樣工作:

Private Sub tsbDown_Click(sender As Object, e As EventArgs) Handles tsbDown.Click 
    With dgvMA 
     If dgvMA.RowCount > 0 Then 
      Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index) 

      dgvMA.ClearSelection() 
      dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1) 
      dgvMA.Rows(MyDesiredIndex).Selected = True 
     End If 
    End With 
End Sub 

Private Function GetNextVisibleCell(currentrow As Integer) As Integer 
    With dgvMA 
     For i = currentrow To .Rows.Count - 1 
      If .Rows(i).Visible = True Then 
       MsgBox(i & ": " & .Rows(i).Visible) 
       Return i 
       Exit For 
      End If 
     Next 

     Return currentrow 
    End With 
End Function 

但永遠都不要我行被選中。 multi-select設置爲falsefullrowselecttrue。當我手動點擊一行或用鍵盤選中時,一切都打印正確。

我在做什麼錯?

回答

0

這應該工作:

Private Sub tsbDown_Click(sender As Object, e As EventArgs) Handles tsbDown.Click 
    With dgvMA 
     If dgvMA.RowCount > 0 And dgvMA.CurrentRow.Index + 1 < dgvMA.Rows.Count Then 
      Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index + 1) 

      dgvMA.ClearSelection() 
      dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1) 
      dgvMA.Rows(MyDesiredIndex).Selected = True 
     End If 
    End With 
End Sub 

Private Sub tsbUp_Click(sender As System.Object, e As System.EventArgs) Handles tsbUp.Click 
    With dgvMA 
     If dgvMA.RowCount > 0 And dgvMA.CurrentRow.Index <> 0 Then 
      Dim MyDesiredIndex As Integer = GetNextVisibleCell(dgvMA.CurrentRow.Index - 1) 

      dgvMA.ClearSelection() 
      dgvMA.CurrentCell = dgvMA.Rows(MyDesiredIndex).Cells(1) 
      dgvMA.Rows(MyDesiredIndex).Selected = True 
     End If 
    End With 
End Sub 

Private Function GetNextVisibleCell(currentrow As Integer) As Integer 
    Dim i As Integer 
    With dgvMA 
     For i = currentrow To .Rows.Count - 1 
      If .Rows(i).Visible = True Then 
       ' MsgBox(i & ": " & .Rows(i).Visible) 
       Return i 
       Exit For 
      End If 
     Next 

     Return currentrow 
    End With 
End Function 

附:我沒有檢查下一個可見單元格的代碼,但它看起來沒問題,行選擇正常工作