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

Lesson 3 Data binding

36 122 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

Định dạng
Số trang 36
Dung lượng 446,32 KB

Nội dung

Chapter Binding, linking, update data in a disconnected environment Lesson 3: Data binding Contents  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 Slide What is data binding?  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 Slide What is data binding? (cont.) Update from textbox to DataTable dataTable.Rows[0][“MaSV”] = textbox.Text; TextBox DataTable Set textbox value from DataTable textbox.Text = dataTable.Rows[0][“MaSV”]; TextBox Data Binding DataTable Slide Types of data binding  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,… Slide Contents  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 Slide Bound-control and unbound-control  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, … Slide Contents  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 Slide Binding data to controls using DataBinding property ControlName.DataBindings.Add( propertyName, DataSource, FieldName )  propertyName: Controls propertyName Label, TextBox, Button… "Text" CheckBox, RadioButton,… "Checked" ComboBox, ListBox,… "SelectedValue"  DataSource:  Can be a table  Can be a BindingSource object  FieldName: column name in database Slide Example  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”); Slide 10 Contents  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 Slide 25 Creating and using DataView object  Creating DataView object  Viewing data using a DataView  Sorting and filtering data using a DataView  Searching data in a DataView Slide 26 Creating DataView object (p.396)  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; Slide 27 View data using a DataView (p.397)  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 Slide 28 View data using a DataView (cont.)  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 Slide 29 View data using a DataView (cont.)  Example: Display data from DataView to ListView control private void HienLenListView( DataView dv, ListView lvw ) { lvw.Items.Clear(); foreach ( DataRowView drv in dv ) { ListViewItem li = lvw.Items.Add(drv[“firstname"].ToString()); li.SubItems.Add(drv[“lastname"].ToString()); } } Slide 30 Sorting and filtering data using a DataView (p.397)  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"; Slide 31 Sorting and filtering data using a DataView (cont.)  To filter a DataView, setting a filter expression in the DataView.RowFilter property  Use the same syntax as the WHERE clause in a SQL query  Example: dataView.RowFilter= "Country='Argentina' AND City='Buenos Aires'"; for: string sql =@"SELECT * FROM Customers WHERE Country='Argentina' AND City='Buenos Aires'"; Slide 32 Searching data in a DataView (p.398)  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 property  Example: DataView dv = new DataView (dsSV.Tables["SinhVien"]); dv.Sort = "MaLop"; DataRowView[] drv = dv.FindRows(txtMaLop.Text); // process drv Slide 34 Example with DataView Slide 35 Contents  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 Slide 36 Navigating records in a DataSet  To navigate records in a DataSet, use BindingSource object  Create BindingSource object BindingSource bs = new BindingSource(dataset, "TableName"); Navigate automatically with BindingNavigator component Navigate manually Slide 37 Navigate automatically  Create BindingSource object  Bind it for BindingNavigator and for other controls  Example: BindingSource bsSV = new BindingSource(dataset, "SinhVien"); dataGridView1.DataSource = bsSV; bindingNavigatorSV.BindingSource = bsSV; lblMaSV.DataBindings.Add("Text", bsSV, "MaSV"); lblMaLop.DataBindings.Add("Text", bsSV, "Malop"); lblDiaChi.DataBindings.Add("Text", bsSV, "diachi"); lblNgaySinh.DataBindings.Add("Text", bsSV, "Ngaysinh"); Slide 38 Navigate manually  Create BindingSource object  Bind it for controls  Using MoveNext, MovePrevious, MoveFirst, MoveLast methods of BindingSource to navigate records Slide 39 Contents  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 Slide 40 ... DataGridView1.DataSource = dataset; DataGridView1.DataMember = "TableName";  Using DataTable object DataGridView1.DataSource = datatable;  Using BindingSource object BindingSource bs = new BindingSource(dataset,... dataTable.Rows[0][“MaSV”]; TextBox Data Binding DataTable Slide Types of data binding  Simple data binding  Binding a single field to a single control property  Some controls for simple data binding: TextBox,... Viewing data using a DataView  Sorting and filtering data using a DataView  Searching data in a DataView Slide 26 Creating DataView object (p .39 6)  The DataView is a customized view of a DataTable

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

w