如何使用Button Click移动到DataGridView中的上一行?

时间:2014-12-19 14:05:40

标签: button datagridview click

我正在尝试使用按钮单击移动到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 个答案:

答案 0 :(得分: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