1. Trang chủ
  2. » Giáo án - Bài giảng

Lesson 3 Data binding

36 123 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Lesson 3 Data Binding
Chuyên ngành Data Binding
Thể loại Lesson
Định dạng
Số trang 36
Dung lượng 446,32 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 Data binding is the process that establishes a connection between the control and data  When controls on a form are data-bound, the underlying data source stays in synch with the valu

Trang 1

Chapter 3

Binding, linking, update data in a disconnected environment

Lesson 3: Data binding

Trang 2

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 3

Data binding is the process that establishes a connection

between the control and data

 When controls on a form are data-bound, the underlying

data source stays in synch with the values in the controls

 When the data changes its value, the controls that are bound

to the data reflect changes automatically

 If an outer representation of the data in an control changes, then the underlying data can be automatically updated to

reflect the change

What is data binding?

Trang 4

What is data binding? (cont.)

Trang 5

 Simple data binding

 Binding a single field to a single control property

 Some controls for simple data binding: TextBox, Label,

CheckBox, RadioButton,…

 Complex data binding

 Binding more than one field to more than one property of a control

 Some controls for complex data binding: ListBox, ComboBox, DataGridView,…

Types of data binding

Trang 6

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 7

 Bound-controls are controls have properties which you can make data binding: DataSource, DisplayMember,

DataMember, DataBindings

 Some controls are data-bound controls : Label, TextBox,

Button, CheckBox, RadioButton, ListBox, ComboBox,

DataGrid, DataGridView…

 Unbound-controls

 Some controls are unbound controls: Timer, ImageList, …

Bound-control and

unbound-control

Trang 8

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 9

 propertyName:

 DataSource:

 Can be a table

 Can be a BindingSource object

 FieldName: column name in database

Binding data to controls using

DataBinding property

ControlName.DataBindings.Add( propertyName, DataSource, FieldName )

Controls propertyName

Label, TextBox, Button… "Text"

CheckBox, RadioButton,… "Checked"

ComboBox, ListBox,… "SelectedValue"

Trang 10

 Example 1:

// create and fill data to dataset and then:

lblMaSV.DataBindings.Add( “Text”, dataset.Tables[“SV”], “MaSV” );

chkPhai.DataBindings.Add( “Checked”, dataset.Tables[“SV”], “Phai” );

 Example 2:

// create and fill data to dataset and then:

BindingSource bs = new BindingSource (dataset, "SV");

lblMaSV.DataBindings.Add (“Text”, bs, “MaSV”);

chkPhai.DataBindings.Add (“Checked”, bs, “Phai”);

Example

Trang 12

 Using DataSet object:

 Using DataTable object

 Using BindingSource object

Binding data to DataGridView,

Trang 13

Example: ComboBox, DataGridView

See more Module

14, Exercise 4

Trang 14

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 15

Working with the DataGridView

(p.425)

 Configuring DataGridView columns

 Adding columns to a DataGridView

 Deleting columns in the DataGridView

 Determining the clicked cell in DataGridView

 Validating input in the DataGridView

 Format a DataGridView using Styles

Trang 16

 There are six built-in types of columns in a DataGridView

Configuring DataGridView

columns

DataGridViewTextBoxColumn Use this column type to display text

and numeric values

DataGridViewCheckBoxColumn Use this column to display Boolean

values

DataGridViewImageColumn Use this column to display images

DataGridViewButtonColumn Use this column to provide users with

a button control

DataGridViewComboBoxColum

n Use this column type to present lists of choices

DataGridViewLinkColumn Use this column type to display links to

other data

Trang 17

 Adding columns to a DataGridView using the Designer

Using the smart tag of the DataGridView

 Adding columns to a DataGridView programmatically

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

Trang 18

 Deleting columns in a DataGridView using the Designer

Using the smart tag of the DataGridView

 Adding columns to a DataGridView programmatically

Call the Remove method and provide the name of the

Trang 19

 To determine the clicked cell, using

 To handle select event, use CellClick event

Determining the clicked cell in DataGridView

private void dgvSV_CellClick( object sender, DataGridViewCellEventArgs e )

{

// using e to define column and row index of current cell

}

Trang 20

Handle the CellValidating event

 CellValidating event is raised when a cell loses focus

 You can Cancel the edit if the value fails validation

private void dgv_CellValidating( object sender,

Trang 21

 DefaultCellStyle property

 AlternatingRowsDefaultCellStyle property

Format a DataGridView using

Styles

Trang 22

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Creating and using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 23

 Creating DataView object

 Viewing data using a DataView

 Sorting and filtering data using a DataView

 Searching data in a DataView

Creating and using DataView

object

Trang 24

 The DataView is a customized view of a DataTable

 Uses:

 Databindable

 Sorting, filtering, searching, editing, and navigation

 Create a new DataView:

 DataView view = new DataView ();

 DataView view = new DataView (DataTable dt);

 Create a DataView reference an existing DataView

 Every DataTable has a default DataView

 DataView view = ds.Tables["Customers"].DefaultView;

Creating DataView object

(p.396)

Trang 25

 Bind DataView object to controls such as the DataGridView

 Example: datagridview.DataSource = dataview;

 Display each column in a DataRowView to individual

controls such as TextBox, Label, ListView,…

 DataView contains a collection of DataRowView objects that represent the rows in the related DataTable

View data using a DataView

(p.397)

Trang 26

 Properties of DataView

 dataView.Count : returns number of rows in DataView

 dataView[int i]: returns row at position ith in DataView, the row type DataRowView

 Properties of DataRowView

 datarowView[string colName]: returns data at column

colName

 datarowView[int index]: returns data at indexth column

View data using a DataView

(cont.)

Trang 27

 Example: Display data from DataView to ListView control

View data using a DataView

lvw.Items.Add(drv[“ firstname "].ToString());

li.SubItems.Add(drv[“ lastname "].ToString());

}

}

Trang 28

To sort a DataView, setting the DataView.Sort property to

the column name you want to sort on

 Use the same syntax as the ORDER BY clause in a SQL query

 Example:

 dataView.Sort = “MaLop DESC, TenLop";

Sorting and filtering data using

a DataView (p.397)

Trang 29

 To filter a DataView, setting a filter expression in the

Sorting and filtering data using

a DataView (cont.)

Trang 30

To search for records in a DataView, using the Find or

FindRows methods

 Pass a value to the Find or FindRows method and both

methods search for that value in the column set in the Sort

Trang 31

Example with DataView

Trang 32

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

 Master/detail relationship

Contents

Trang 33

 To navigate records in a DataSet, use BindingSource object

 Create BindingSource object

Trang 34

 Create BindingSource object

 Bind it for BindingNavigator and for other controls

Trang 35

 Create BindingSource object

 Bind it for controls

 Using MoveNext, MovePrevious, MoveFirst, MoveLast

methods of BindingSource to navigate records

Navigate manually

Trang 36

 Data binding

 Bound-control and unbound-control

 Binding data to controls

 Working with the DataGridView control

 Using DataView object

 Navigating records in a DataSet

Contents

Ngày đăng: 13/05/2014, 12:19

w