public Sub New(dt As DataTable)
{
Datagridview1.DataSource = dt
}
用法:
Dim dt As DataTable = AMethodToReturnDataTableObject()
Using frm As new Form2(dt)
//wait for user response
Dim dlg As DialogResult = frm.ShowDialog()
End Using
注1: AMethodToReturnDataTableObject 可能看起来像:
Public Function GetDataTable(sql As String, para As OleDbParameter()) As DataTable
Dim dt As DataTable = New DataTable()
Using oConn As OleDbConnection = New OleDbConnection(sConn)
oConn.Open()
Using oComm As OleDbCommand = New OleDbCommand(sql, oConn)
For Each odp As OleDbParameter in para
oComm.Parameters.Add(odp)
Using oRdr As OleDbDataReader = oComm.ExecuteReader()
dt.Load(oRdr)
End Using
End Using
Return dt
End Function
如您所见,上面的方法获取一个查询字符串和 OleDbParameter 的。用法:
Dim fromdate As DateTime = New DateTime(1966,1,1)
Dim todate As DateTime = New DateTime(1978,12,31)
Dim ps As OleDbParameter() = New OleDbParameter() _
{
New OleDbParameter() With {.OleDbType = OleDbType.Date, .Value = fromdate}, _
New OleDbParameter() With {.OleDbType = OleDbType.Date, .Value = todate} _
}
Dim sComm As String = "SELECT Mu.* FROM MyUsers AS Mu WHERE Mu.DateOfBirth BETWEEN ? AND ? ORDER BY Mu.DateOfBirth;"
Dim dt As DataTable = DbHelper.AMethodToReturnDataTableObject(sComm, ps)
注意#2:oledb provider for ms access数据库使用未命名的参数。所以,参数的顺序非常重要! 祝你好运!
1条答案
按热度按时间g52tjvyc1#
我建议开始阅读文档:datagridview控件(windows窗体)。
msdn声明:
datagridview控件支持标准的windows窗体数据绑定模型,因此它将绑定到以下列表中描述的类的示例:
实现
IList
接口,包括一维数组。实现
IListSource
接口,例如DataTable
以及DataSet
班级。实现
IBindingList
接口,例如BindingList<T>
班级。实现
IBindingListView
接口,例如BindingSource
班级。如您所见,在datagridview控件中显示数据有多种方法。其中之一是将数据抓取到datatable中,然后绑定datagridview的
Datatasource
属性:[编辑]
在第二种形式中,您必须更改构造函数:
用法:
注1:
AMethodToReturnDataTableObject
可能看起来像:如您所见,上面的方法获取一个查询字符串和
OleDbParameter
的。用法:注意#2:oledb provider for ms access数据库使用未命名的参数。所以,参数的顺序非常重要!
祝你好运!