扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
添加一个DataGridView控件,在控件右上的小三角型点一下,选择数据源--添加数据源--(数据库)下一步--新建联接--(浏览一个已存在的数据库文件)确定--下一步(可选不复制)--下一步--数据集包含数据库对象选择表中你的自定义表名(希望显示的表)--完成
城口ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
上面完成启动调试就能把数据库的表读到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
一般,我都不喜欢做这样报上一条下一条,太专业的数据库操作不适合终端用户。
如果必须要做,你可以这样:按排序规则,取到健列表。保存到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数据库名字.mdb")
'定义连接
Public
DataBaseRST
As
Integer
'用来返回数据库执行结果
Public
Function
DataModify(ByVal
str
As
String)
As
Boolean
'进行数据库修改操作
Dim
cmdinsert
As
New
OleDbCommand
Try
cmdinsert.CommandText
=
str
cmdinsert.Connection
=
cn
If
cn.State
=
ConnectionState.Closed
Then
cn.Open()
DataBaseRST
=
cmdinsert.ExecuteNonQuery()
'用来返回执行的结果
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,用来显示数据的控件
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
'以下是调用方法
DataModify("
insert
into
aa
values
('1','2')")'-------这里是数据库更新操作
Search("select
bb
from
aa",DataGridView1)'-----------这里是数据表查询操作
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流