添加一個DataGridView控件,在控件右上的小三角型點一下,選擇數(shù)據(jù)源--添加數(shù)據(jù)源--(數(shù)據(jù)庫)下一步--新建聯(lián)接--(瀏覽一個已存在的數(shù)據(jù)庫文件)確定--下一步(可選不復制)--下一步--數(shù)據(jù)集包含數(shù)據(jù)庫對象選擇表中你的自定義表名(希望顯示的表)--完成
城口ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
上面完成啟動調(diào)試就能把數(shù)據(jù)庫的表讀到datagridview中顯示
添加下面代碼到代碼項就能單擊datagridview中記錄時textbox中同步顯示
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
TextBox1.Text = DataGridView1.SelectedCells(0).Value
End Sub
一般,我都不喜歡做這樣報上一條下一條,太專業(yè)的數(shù)據(jù)庫操作不適合終端用戶。
如果必須要做,你可以這樣:按排序規(guī)則,取到健列表。保存到list中,上下條,只是改變一索引而已。
Imports System.Data.OleDb
Class BindNavigate
Dim cnn As OleDb.OleDbConnection '打開連接略
Dim lst As New List(Of Integer)
Sub New()
lst = New List(Of Integer)
index = -1
Using da As New OleDbDataAdapter("select id from mytable order by abc,def", cnn), tb As New DataTable
da.Fill(tb)
For Each row As DataRow In tb.Rows
Dim n As Integer = row(0)
lst.Add(n)
Next
If tb.Rows.Count 0 Then CurrentIndex = 0
End Using
End Sub
Dim index As Integer Event CurrentIndexChanged()
Property CurrentIndex As Integer
Get
Return index
End Get
Set(ByVal value As Integer)
Dim b As Boolean = value index
index = value
If b Then RaiseEvent CurrentIndexChanged()
End Set
End Property
'當前的鍵值
ReadOnly Property CurrentValue As Integer
Get
Return lst(index)
End Get
End Property
Sub MoveFirst()
CurrentIndex = 0
End Sub
Sub MovePrevious()
CurrentIndex -= 1
End Sub
Sub MoveNext()
CurrentIndex += 1
End Sub
Sub MoveLast()
CurrentIndex = lst.Count - 1
End Sub
ReadOnly Property BOF
Get
Return CurrentIndex = 0
End Get
End Property
ReadOnly Property EOF
Get
Return CurrentIndex = lst.Count - 1
End Get
End Property
Private Sub Text_CurrentIndexChanged() Handles Me.CurrentIndexChanged
'綁定過程
End Sub
End Class
以下是完整模塊
Imports
System.Data
Imports
System.IO
Imports
System.Data.OleDb
Module
Module1
Public
cn
As
New
OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Application.StartupPath
"\mdb數(shù)據(jù)庫名字.mdb")
'定義連接
Public
DataBaseRST
As
Integer
'用來返回數(shù)據(jù)庫執(zhí)行結(jié)果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'進行數(shù)據(jù)庫修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用來返回執(zhí)行的結(jié)果
cn.Close()
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
Public
Function
Search(ByVal
str
As
String,
ByVal
DGV
As
DataGridView)
As
Boolean
'查詢
str---查詢命令,DGV---DataGridView,用來顯示數(shù)據(jù)的控件
Dim
tb
As
New
DataTable
Try
Dim
ap
As
New
OleDb.OleDbDataAdapter(str,
cn)
ap.Fill(tb)
DGV.DataSource
=
tb
Return
True
Catch
ex
As
Exception
MessageBox.Show(Err.Description,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
False
End
Try
End
Function
End
Module
'以下是調(diào)用方法
DataModify("
insert
into
aa
values
('1','2')")'-------這里是數(shù)據(jù)庫更新操作
Search("select
bb
from
aa",DataGridView1)'-----------這里是數(shù)據(jù)表查詢操作