Chapter 8 Lesson 1: Creating a Data-Bound Form with the Data Sources Wizard Lesson 2: Implementing Data-Bound Controls Lesson 3: Working with the DataGridView... Lesson 1: Creating a
Trang 1Chapter 8
Implementing Data-Bound Controls
Trang 2Chapter 8
Lesson 1: Creating a Data-Bound Form with the Data Sources Wizard
Lesson 2: Implementing Data-Bound
Controls
Lesson 3: Working with the DataGridView
Trang 3Lesson 1: Creating a Data-Bound Form with the Data Sources Wizard
What is Data-Bound controls?
– Bound and Un-Bound mode
– Data-bound controls are WinForms controls those can
easily bind with data components: DataGrid, ListBox, and a ComboBox
– have properties, which you can set as a data component
and theyre ready to present your data in WinForms:
DataSource,DisplayMember, DataMember
Dataset (Rows)
ID, Name… Dataset (Rows)Dataset (Rows)
Dataset (Rows) Position???
Trang 4Lesson 1: Creating a Data-Bound Form with the Data Sources Wizard
DEMO
– Creating a Data-Bound Windows Form
– Ex p411
Trang 5Track record position
– MoveNext, MovePrevious…
Change data in datasource
– Add, Delete, EndEdit
Dataset (Rows)
ID, Name… Dataset (Rows)Dataset (Rows)
Dataset (Rows) BindinhSource
Trang 6Lesson 2: Implementing Data-Bound Controls
– Simple data binding: displaying a single element
BindingSource productsBindingSource = New BindingSource(NorthwindDataSet1,
“Products");
TextBox1.DataBindings.Add("Text", productsBindingSource, "ProductName");
Trang 7Lesson 2: Implementing Data-Bound Controls
Binding Controls to Data
– Complex data binding: binding a control to more than one source of data
BindingSource customersBindingSource=New BindingSource(NorthwindDataSet1, "Customers");
DataGridView1.DataSource = customersBindingSource;
– Or
DataGridView1.DataSource = NorthwindDataSet1;
DataGridView1.DataMember = "Customers“;
Trang 8Lesson 2: Implementing Data-Bound Controls
Navigating Records in a DataSet
– use a BindingNavigator component.
– Assign the BindingNavigator.BindingSource
property to BindingSource component
Trang 9Lesson 3: Binding with Listbox/Combobox
Using unBound-mode
– Add items manually
Using bound-mode
– Single table
– Two tables
Core properties
– Datasource
– DataMember
– Displaymember
– ValueMember
SeletectedValue
– BindingSource
– Example
Trang 10Lesson 3: Binding with Listbox/Combobox
Binding with single table
– DataSource
– DisplayMember
– ValueMember
Return value:
– SelectedValue
Text
Trang 11Lesson 3: Binding with Listbox/Combobox
Binding with 2 tables
– DataSource
– DisplayMember
– ValueMember
Binding with
– SelectedValue
Trang 12Lesson 3: Working with the
DataGridView
Configuring DataGridView Columns
Trang 13Lesson 3: Working with the
DataGridView
Adding Columns to a DataGridView
– Wizard (design time)
Adding Columns to a DataGridView (p428)
– Dim ProductNameColumn As New
DataGridViewTextBoxColumn
– ProductNameColumn.Name = "ProductName"
– ProductNameColumn.HeaderText = "Product Name"
– ProductNameColumn.ValueType =
System.Type.GetType("System.String")
– DataGridView1.Columns.Add(ProductNameColumn)
Trang 14Lesson 3: Working with the
DataGridView
Determining the Clicked Cell in
DataGridView
– use the DataGridView.CurrentCell property
Dim CurrentCellValue As String
CurrentCellValue = CustomersDataGridView.CurrentCell.Value.ToString
Trang 15Lesson 3: Working with the
DataGridView
ErrorText proerty of row and cell in row
Validating Input in the DataGridView
– handle the DataGridView CellValidating event and cancel the edit if the value fails validation (is raised when a cell loses focus.)
Trang 16Lesson 3: Working with the
DataGridView
Format a DataGridView Using Styles
– DefaultCellStyle
– AlternatingRowsDefaultCellStyle
Lab: Working with the DataGridView
– Page 431