2014-12-19 58 views
2

我想要使用按鈕單擊移動到DataGridView中的前一行。首先點擊它移動到前一行,但第二次單擊它在同一行上。任何人都可以幫忙嗎?如何使用按鈕點擊移動到DataGridView中的前一行?

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click 
    Dim i As Integer = DataGridView1.CurrentRow.Index - 1 

    TextBox1.Text = DataGridView1.Item(0, i).Value 
    TextBox2.Text = DataGridView1.Item(1, i).Value 

    DataGridView1.ClearSelection() 
    DataGridView1.Rows(i).Selected = True 

End Sub 

回答

1

你必須設置CurrentCell爲了改變這是隻讀屬性CurrentRow

Dim i As Integer = DataGridView1.CurrentRow.Index - 1 

TextBox1.Text = DataGridView1.Item(0, i).Value 
TextBox2.Text = DataGridView1.Item(1, i).Value 

Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(i).Cells(0) 
Me.DataGridView1.Rows(i).Selected = True 
+0

感謝隊友它像一個魅力 –