我想将SQL Server表中的数据插入到数据网格中。
这是我的代码:
Dim cnConnect As New SqlConnection
cnConnect.ConnectionString = conn.ConnectionString
cnConnect.Open()
Dim cm As New SqlCommand
cm.CommandText = "SELECT * FROM user"
Dim Adpt As New SqlDataAdapter(cm.CommandText, cnConnect)
Dim ds As New DataSet
Adpt.Fill(ds, "user")
DataGrid2.DataBind()
我尝试将数据绑定到数据网格。但结果是空白的
答案 0 :(得分:3)
您实际上没有向要绑定的网格提供任何数据。您必须首先设置DataSource
,以便在您致电DataBind
时网格有一个要绑定的来源。
答案 1 :(得分:0)
简单一点,尝试这种编码风格
Dim cnConnect As New SqlConnection
cnConnect.ConnectionString = conn.ConnectionString
cnConnect.Open()
Dim cm As New SqlCommand
Dim dt AS DataTable
cm.CommandText = "SELECT * FROM user"
cm.Connection = cnConnect
dt.Load(cm.ExecuteReader())
DataGrid2.DataSource = dt
DataGrid2.DataBind()