chapter 8_databound controls

Upload: vuong-pham

Post on 07-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Chapter 8_databound Controls

    1/16

    Chapter 8

    Implementing Data-Bound Controls

  • 8/3/2019 Chapter 8_databound Controls

    2/16

    Chapter 8

    Lesson 1: Creating a Data-Bound Form withthe Data Sources Wizard

    Lesson 2: Implementing Data-BoundControlsLesson 3: Working with the DataGridView

  • 8/3/2019 Chapter 8_databound Controls

    3/16

    Lesson 1: Creating a Data-Bound Formwith the Data Sources Wizard

    What is Data-Bound controls? Bound and Un-Bound mode

    Data-bound controls are WinForms controls those caneasily bind with data components: DataGrid, ListBox, and aComboBox

    have properties, which you can set as a data componentand theyre ready to present your data in WinForms:DataSource,DisplayMember, DataMember.

    Dataset (Rows)

    ID, Name

    Dataset (Rows)

    Dataset (Rows)

    Dataset (Rows)

    Dataset (Rows)Position???

  • 8/3/2019 Chapter 8_databound Controls

    4/16

    Lesson 1: Creating a Data-Bound Formwith the Data Sources Wizard

    DEMO Creating a Data-Bound Windows Form

    Ex p411

  • 8/3/2019 Chapter 8_databound Controls

    5/16

    BinDingSource

    Track record position MoveNext, MovePrevious

    Change data in datasource Add, Delete, EndEdit

    Dataset (Rows)ID, Name Dataset (Rows)Dataset (Rows)Dataset (Rows)

    Dataset (Rows)

    BindinhSource

  • 8/3/2019 Chapter 8_databound Controls

    6/16

    Lesson 2: Implementing Data-BoundControls

    Binding Controls to Data Simple data binding: displaying a single

    elementBindingSource productsBindingSource = NewBindingSource(NorthwindDataSet1,Products");

    TextBox1.DataBindings.Add("Text",productsBindingSource, "ProductName");

  • 8/3/2019 Chapter 8_databound Controls

    7/16

    Lesson 2: Implementing Data-BoundControls

    Binding Controls to Data Complex data binding: binding a control to more

    than one source of data

    BindingSource customersBindingSource=NewBindingSource(NorthwindDataSet1, "Customers");DataGridView1.DataSource = customersBindingSource;

    Or DataGridView1.DataSource = NorthwindDataSet1;DataGridView1.DataMember = "Customers;

  • 8/3/2019 Chapter 8_databound Controls

    8/16

    Lesson 2: Implementing Data-BoundControls

    Navigating Records in a DataSet use a BindingNavigator component.

    Assign the BindingNavigator.BindingSourceproperty to BindingSource component

  • 8/3/2019 Chapter 8_databound Controls

    9/16

    Lesson 3: Binding withListbox/Combobox

    Using unBound-mode Add items manually

    Using bound-mode Single table Two tables

    Core properties Datasource DataMember Displaymember ValueMember

    SeletectedValue

    BindingSource

    Example

  • 8/3/2019 Chapter 8_databound Controls

    10/16

    Lesson 3: Binding withListbox/Combobox

    Binding with single table DataSource

    DisplayMember ValueMember

    Return value: SelectedValue

    Text

  • 8/3/2019 Chapter 8_databound Controls

    11/16

    Lesson 3: Binding withListbox/Combobox

    Binding with 2 tables DataSource

    DisplayMember ValueMember

    Binding with SelectedValue

  • 8/3/2019 Chapter 8_databound Controls

    12/16

    Lesson 3: Working with theDataGridView

    Configuring DataGridView Columns

  • 8/3/2019 Chapter 8_databound Controls

    13/16

    Lesson 3: Working with theDataGridView

    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)

  • 8/3/2019 Chapter 8_databound Controls

    14/16

    Lesson 3: Working with theDataGridView

    Determining the Clicked Cell inDataGridView

    use the DataGridView.CurrentCell propertyDim CurrentCellValue As StringCurrentCellValue =CustomersDataGridView.CurrentCell.Value.ToString

  • 8/3/2019 Chapter 8_databound Controls

    15/16

    Lesson 3: Working with theDataGridView

    ErrorText proerty of row and cell in rowValidating Input in the DataGridView

    handle the DataGridView .CellValidating eventand cancel the edit if the value fails validation (israised when a cell loses focus.)

  • 8/3/2019 Chapter 8_databound Controls

    16/16

    Lesson 3: Working with theDataGridView

    Format a DataGridView Using Styles DefaultCellStyle

    AlternatingRowsDefaultCellStyleLab: Working with the DataGridView Page 431