vb.net datagridview绑定代码
DimconnAsNewSystem.Data.OleDb.OleDbConnection()DimstrconnAsStringDimcmdAsNewSystem.Da...
Dim conn As New System.Data.OleDb.OleDbConnection()
Dim strconn As String
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ada As New System.Data.OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
Dim TableIndex As Integer
Dim dt As New DataTable
Dim bs As New BindingSource
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =D:\\insert\db1.mdb"
conn.ConnectionString = strconn
conn.Open()
strconn = "select * from 部门テーブル"
DataGridView1.DataSource = strconn
怎么绑定才能显示啊各位大大帮帮忙 展开
Dim strconn As String
Dim cmd As New System.Data.OleDb.OleDbCommand()
Dim ada As New System.Data.OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
Dim TableIndex As Integer
Dim dt As New DataTable
Dim bs As New BindingSource
strconn = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source =D:\\insert\db1.mdb"
conn.ConnectionString = strconn
conn.Open()
strconn = "select * from 部门テーブル"
DataGridView1.DataSource = strconn
怎么绑定才能显示啊各位大大帮帮忙 展开
3个回答
展开全部
'引用命名空间
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class frmDataShow
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
'数据库连接
Dim strConnection As String = "server=waly-pc;database=Login;uid=sa;pwd=123456;"
Dim sqlConnection1 As New SqlConnection(strConnection)
Dim dataAdapter As New SqlDataAdapter
Dim dst As New DataSet
Dim dt As New DataTable
sqlConnection1.Open() '打开数据库
Dim sql As String = "select * from UserInfo"
Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)
dataAdapter.SelectCommand = cmd
dataAdapter.Fill(dst, "info") '将数据库查询的数据绑定datatable
dt = dst.Tables("info")
sqlConnection1.Close() '关闭数据库
DataGridView1.AutoGenerateColumns = True '自动创建列
DataGridView1.DataSource = dt '将数据库查询的数据绑定到DataGridView1
End Sub
End Class
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class frmDataShow
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
'数据库连接
Dim strConnection As String = "server=waly-pc;database=Login;uid=sa;pwd=123456;"
Dim sqlConnection1 As New SqlConnection(strConnection)
Dim dataAdapter As New SqlDataAdapter
Dim dst As New DataSet
Dim dt As New DataTable
sqlConnection1.Open() '打开数据库
Dim sql As String = "select * from UserInfo"
Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)
dataAdapter.SelectCommand = cmd
dataAdapter.Fill(dst, "info") '将数据库查询的数据绑定datatable
dt = dst.Tables("info")
sqlConnection1.Close() '关闭数据库
DataGridView1.AutoGenerateColumns = True '自动创建列
DataGridView1.DataSource = dt '将数据库查询的数据绑定到DataGridView1
End Sub
End Class
展开全部
你用的strconn是String,而DataGridView1.DataSource要绑定的对象必须是DataSet对象,而且你的查询指令都没有执行,更不可能会与DataGridView1控件绑定。应该改一下:
================================
Try
Using DataCon As New OleDb.OleDbConnection(strconn)
Dim ada As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter( "select * from 部门テーブル", DataCon)
ada.Fill(DT)
End Using
Catch ex As Exception
End If
End Try
DataGridView1.DataSource = dt
===================================
应该就可以解决你的问题了。
================================
Try
Using DataCon As New OleDb.OleDbConnection(strconn)
Dim ada As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter( "select * from 部门テーブル", DataCon)
ada.Fill(DT)
End Using
Catch ex As Exception
End If
End Try
DataGridView1.DataSource = dt
===================================
应该就可以解决你的问题了。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim dataset As DataSet
dataset = New DataSet
Try
Dim Dataadapter As New System.Data.SqlClient.SqlDataAdapter(strconn , conn)
Dataadapter.Fill(dataset, "select_table")
DataGridView1.DataSource = dataset
DataGridView1.DataMember = "select_table"
conn.Close()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
dataset = New DataSet
Try
Dim Dataadapter As New System.Data.SqlClient.SqlDataAdapter(strconn , conn)
Dataadapter.Fill(dataset, "select_table")
DataGridView1.DataSource = dataset
DataGridView1.DataMember = "select_table"
conn.Close()
conn.Dispose()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询