问题是,无论我从哪个位置开始,每次按下时它都从第一个记录开始,然后正确地向前移动,而我希望它从我所在的位置开始。我真的是编程新手,已经为此苦苦挣扎了两天了:(这是该表格的代码
Imports System.Data.OleDb
Imports System.IO
Public Class Details
Private Sub ShowData(CurrentRow)
Try
TbxSName.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Scientific_Name")
TbxCName.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Common_Name")
TbxDescription.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Description")
TbxEdibilty.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Edibility")
TbxLocation.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Location")
TbxMorphology.Text = Dst.Tables("Mushrooms").Rows(CurrentRow)("Morphology")
Catch ex As Exception
MsgBox(ex.Message, "error")
End Try
End Sub
Sub clr()
TbxSName.Clear()
TbxCName.Clear()
TbxDescription.Clear()
TbxEdibilty.Clear()
TbxLocation.Clear()
TbxMorphology.Clear()
End Sub
Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call connection()
Currentrow = 0
Dad = New OleDbDataAdapter("SELECT * FROM Mushrooms", cn)
Dad.Fill(Dst, "Mushrooms")
ShowData(Currentrow)
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
Call connection()
If Currentrow = Dst.Tables("Mushrooms").Rows.Count - 1 Then
MsgBox("Last Record is Reached", MsgBoxStyle.Exclamation)
Else
Currentrow += 1
ShowData(Currentrow)
End If
cn.Close()
End Sub
End Class
不要从某个地方呼叫连接。在使用它的地方创建它。连接需要关闭和处理。即使出现错误,使用块也可以为您完成此操作。在显示的代码中,您似乎不需要DataAdapter或DataSet。只需使用数据表和命令即可。
由于您尚未共享声明Currentrow的位置或方式,因此我无法告诉您为什么它没有更新。另外,您还没有共享ShowData方法,所以我无法确定那里是否出了问题。
Private conString As String = "Your connection string"
Private CurrentRow As Integer
Private dt As DataTable
Private Sub Details_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt = New DataTable
Using cn As New OleDbConnection(conString),
cmd As New OleDbCommand("SELECT * FROM Mushrooms", cn)
dt.Load(cmd.ExecuteReader)
End Using
CurrentRow = 0
ShowData()
End Sub
Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
If CurrentRow >= dt.Rows.Count - 1 Then
MessageBox.Show("Last Record is Reached")
Else
CurrentRow += 1
ShowData()
End If
End Sub
Private Sub ShowData()
TbxSName.Text = dt.Rows(CurrentRow)("Scientific_Name").ToString
TbxCName.Text = dt.Rows(CurrentRow)("Common_Name").ToString
TbxDescription.Text = dt.Rows(CurrentRow)("Description").ToString
TbxEdibilty.Text = dt.Rows(CurrentRow)("Edibility").ToString
TbxLocation.Text = dt.Rows(CurrentRow)("Location").ToString
TbxMorphology.Text = dt.Rows(CurrentRow)("Morphology").ToString
End Sub