图片附件: 游客没有浏览图片的权限,请
登录 或
注册
是动态绑定的,我刚从VB6.0转过来,为了熟悉数据库,做了这样一个小程序,界面如上
单击“连接数据库”则显示数据所含表
单击“打开并绑定表”则显示包含字段名称并绑定到datagridview1
可以通过包含字段查看,该字段的类型!
现在其它功能都成了,只是更改表后,datagridview1数据不同步
Public ds As New Data.DataSet()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click
Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
Dim tabname As String = cmbtable.Text
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim sql As String = "select * from " & tabname
conn.Open()
cmd.Connection = conn
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
ds.Clear()
da.Fill(ds, "pylj")
DataGridView1.DataSource = Nothing
Dim k As Integer = DataGridView1.ColumnCount - 1
For j As Integer = 0 To k
DataGridView1.Columns.Clear()
DataGridView1.Columns.Remove(DataGridView1.SelectedColumns(j))
Next
DataGridView1.Refresh()
MsgBox("d")
DataGridView1.DataSource = ds.Tables(0)
Dim n As Integer = ds.Tables(0).Columns.Count - 1
Dim y As Integer = cmbfiled.Items.Count - 1
For x As Integer = 0 To y
cmbfiled.Items.Clear()
Next
cmbfiled.Refresh()
For i As Integer = 0 To n
cmbfiled.Items.Add(ds.Tables(0).Columns(i).ColumnName)
Next
cmbfiled.SelectedIndex = 0
End Sub
这是打开表并绑定的代码,为了方便,把ds数据集设成全局变量
请高手指点
[
本帖最后由 shenhaoli 于 2010-4-18 18:19 编辑 ]