Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Windows Forms DataGrid A Complex-Bound .NET GUI Control MikeFITZSIMON SYSTEMSARCHITECT FITZSIMON IT CONSULTING PTY LTD Simple vs Complex Binding simple binding : binding a control to a single data column. A TextBox is an example of a control that can be simply bound. complex binding : binding more than one column to a single control. The DataGrid is an example of a control that can be complexly bound. Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Retrieving and Updating Data Add a new connection (in Server Explorer, right-click Data Connections and choose Add Connection) Connect the form to the tables (in Server Explorer, drag table icons to the form. An OleDbDataAdapter is created for each table, and an OleDbConnection to the database is also added) Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Demo Northwind.mdb Three tables: Customers, Orders, Order Details Display in DataGrid Update in DataGrid Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Danger, Will Robinson! The three tables are related in the database, but no relations exist between them in the DataSet. Editing in the DataGrid can create inconsistent data in the DataSet. dataAdapter.Update will fail Avoid this by adding DataRelation objects to your DataSet Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Proper Case Column Headers When setting a DataGrid’s data source programmatically, cannot assign column header names at design time. The DataGrid uses the recordset’s column names. Often these recordset column names may be confusing, not to mention unprofessional looking. Work-around: use alias field names in the SQL statement for the underlying data source. E.g., in a SQL statement, instead of: SELECT pub_id, pub_name FROM pubs Use: SELECT pub_id AS ID, pub_name AS Publisher FROM pubs Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Data binding with a collection A collection can be populated with simple data, such as strings, and bound to a list box control. Dim objCollection As New Collection() objCollection.Add("Value objCollection.Add("Value objCollection.Add("Value objCollection.Add("Value 1") 2") 3") 4") ListBox1.DataSource = objCollection Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Custom objects bound directly to a DataGrid control Create a new class Public Class MyData Private cstrValue1 As String Private cstrValue2 As String Public Sub New(ByVal Value1 As String, ByVal Value2 As String) cstrValue1 = Value1 cstrValue2 = Value2 End Sub Public Property Value1() As String Get Return cstrValue1 End Get Set(ByVal Value As String) cstrValue1 = Value End Set End Property Public Property Value2() As String Get Return cstrValue2 End Get Set(ByVal Value As String) cstrValue2 = Value End Set End Property End Class Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Custom objects bound directly to a DataGrid control Next, add a data grid control to the form, and add the following code to the form Load event: Dim objCollection As New Collection() objCollection.Add(New objCollection.Add(New objCollection.Add(New objCollection.Add(New MyData("Value MyData("Value MyData("Value MyData("Value 1", 2", 3", 4", DataGrid1.DataSource = objCollection Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au "Test "Test "Test "Test 1")) 2")) 3")) 4")) Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au Questions Mike Fitzsimon [email protected] Sample code & this ppt available from www.fitzsimon.com.au/qmsdnug Fitzsimon IT CONSULTING PTY LTD www.fitzsimon.com.au