我的主要目標是撰寫導航按鈕來控制與 Microsoft Access 資料庫連接的vb.net專案的記錄。包括的四個按鈕是第一條記錄、最后一條記錄、下一條記錄和上一條記錄。顯示的錯誤說“ShowData”和“CurrentRow”沒有宣告。

CurrentRow 顯示以下糾錯選項:

ShowData 顯示以下糾錯選項:

'to navigate to first record
Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
CurrentRow = 0
ShowData(CurrentRow)
End Sub
'to navigate to last record
Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1
ShowData(CurrentRow)
End Sub
'to navigate to previous record
Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
If CurrentRow <> 0 Then
CurrentRow -= 1
ShowData(CurrentRow)
Else
MsgBox("First record is reached!", MsgBoxStyle.Exclamation)
End If
End Sub
'to navigate to next record
Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
If CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1 Then
MsgBox("Last record is reached!", MsgBoxStyle.Exclamation)
Else
CurrentRow = 1
ShowData(CurrentRow)
End If
End Sub
uj5u.com熱心網友回復:
根據您得到的錯誤,您在方法中使用它之前沒有宣告 CurrentRow。放
Private CurrentRow as Integer
在你的課堂上。ShowData 呼叫該方法從資料庫中檢索資料。它應該是這樣的:
Private Sub ShowData(byval row as integer)
'code to get the data in the database
End Function
如果變數和方法存在于專案的另一個類中,您需要通過它們的類來參考它們。例如:
ClassName.CurrentRow
ClassName.ShowData(CurrentRow)
uj5u.com熱心網友回復:
我的 Roasters 表的欄位名稱為 Name、City 和 Zip。它們系結到BindingNavigator表單上的標簽,也是一個DataGridView. 在BindingNavigator提供您所描述的按鈕。
匯入 System.Data.SqlClient
公共類 BindingNavigator
Dim WithEvents bindSrc As New BindingSource
Private Sub BindingNavigator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = LoadData1()
bindSrc.DataSource = dt
BindingNavigator1.BindingSource = bindSrc
lblItem.DataBindings.Add(New Binding("Text", bindSrc, "Name"))
lblDescription.DataBindings.Add(New Binding("Text", bindSrc, "City"))
lblPrice.DataBindings.Add(New Binding("Text", bindSrc, "Zip"))
DataGridView1.DataSource = bindSrc
End Sub
Private Function LoadData1() As DataTable
Dim dt As New DataTable
Using conn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Top 10 * From Roasters;", conn)
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
結束類
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/320886.html
上一篇:前100個偶數和奇數的和?
