本文接着上文[DevExpress WinForms控件] 如何绑定到SQL数据?(二)介绍
三、使用代码绑定到SQL数据 以下代码说明了如何使用SqlDataSource组件在运行时将GridControl绑定到SQL数据。 在此示例中,使用的是CustomSqlQuery。要了解其他查询类型,请参阅SelectQuery和StoredProcQuery类的说明。 要了解支持的数据库引擎,请参阅DataConnectionParametersBase的继承层次结构。 [C#]using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Sql; // . . . // Create a data source. Access97ConnectionParameters connectionParameters = new Access97ConnectionParameters("D:\\Work\\nwind.mdb", "", ""); SqlDataSource ds = new SqlDataSource(connectionParameters); // Create an SQL query to access the Products table. CustomSqlQuery query = new CustomSqlQuery(); query.Name = "customQuery1"; query.Sql = "SELECT [Products].[ProductID], [Products].[ProductName], [Products].[QuantityPerUnit], [Products].[UnitPrice], " + "[Products].[UnitsInStock], [Products].[UnitsOnOrder], [Products].[ReorderLevel], [Products].[Discontinued] " +"FROM [Products] [Products]"; ds.Queries.Add(query); ds.Fill(); //Assign the data source for the grid and retrieve fields. gridControl1.DataSource = ds; gridControl1.DataMember = "customQuery1";[VB]
Imports DevExpress.DataAccess.ConnectionParameters Imports DevExpress.DataAccess.Sql ' . . . ' Create a data source. Private connectionParameters As New Access97ConnectionParameters("D:\Work\nwind.mdb", "", "") Private ds As New SqlDataSource(connectionParameters) ' Create an SQL query to access the Products table. Private query As New CustomSqlQuery() query.Name = "customQuery1" query.Sql = "SELECT [Products].[ProductID], [Products].[ProductName], [Products].[QuantityPerUnit], [Products].[UnitPrice], " & "[Products].[UnitsInStock], [Products].[UnitsOnOrder], [Products].[ReorderLevel], [Products].[Discontinued] " & "FROM [Products] [Products]" ds.Queries.Add(query) ds.Fill() 'Assign the data source for the grid and retrieve fields. gridControl1.DataSource = ds gridControl1.DataMember = "customQuery1"请注意,在控件绑定到数据源之后,具体控件可能需要其他自定义。有关更多信息,请参阅相应控件的文档。
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/900.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/900.html