Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Windows Forms DataGrid A Complex-Bound.NET GUI Control Mike FITZSIMON SYSTEMSARCHITECT F ITZSIMON IT C ONSULTING PTY LTD.

Similar presentations


Presentation on theme: "The Windows Forms DataGrid A Complex-Bound.NET GUI Control Mike FITZSIMON SYSTEMSARCHITECT F ITZSIMON IT C ONSULTING PTY LTD."— Presentation transcript:

1 The Windows Forms DataGrid A Complex-Bound.NET GUI Control Mike FITZSIMON SYSTEMSARCHITECT F ITZSIMON IT C ONSULTING PTY LTD

2 Fitzsimon IT C ONSULTING PTY LTD www.fitzsimon.com.au 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.

3 Fitzsimon IT C ONSULTING 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)

4 Fitzsimon IT C ONSULTING PTY LTD www.fitzsimon.com.au Demo Northwind.mdb Three tables: Customers, Orders, Order Details Display in DataGrid Update in DataGrid

5 Fitzsimon IT C ONSULTING 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

6 Fitzsimon IT C ONSULTING 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

7 Fitzsimon IT C ONSULTING 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 1") objCollection.Add("Value 2") objCollection.Add("Value 3") objCollection.Add("Value 4") ListBox1.DataSource = objCollection

8 Fitzsimon IT C ONSULTING 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

9 Fitzsimon IT C ONSULTING 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 MyData("Value 1", "Test 1")) objCollection.Add(New MyData("Value 2", "Test 2")) objCollection.Add(New MyData("Value 3", "Test 3")) objCollection.Add(New MyData("Value 4", "Test 4")) DataGrid1.DataSource = objCollection

10 Fitzsimon IT C ONSULTING PTY LTD www.fitzsimon.com.au

11 Questions Mike Fitzsimon Mike@Fitzsimon.com.au Sample code & this ppt available from www.fitzsimon.com.au/qmsdnug


Download ppt "The Windows Forms DataGrid A Complex-Bound.NET GUI Control Mike FITZSIMON SYSTEMSARCHITECT F ITZSIMON IT C ONSULTING PTY LTD."

Similar presentations


Ads by Google