Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHƯƠNG VII: KẾT GÁN DỮ LIỆU

Similar presentations


Presentation on theme: "CHƯƠNG VII: KẾT GÁN DỮ LIỆU"— Presentation transcript:

1 CHƯƠNG VII: KẾT GÁN DỮ LIỆU
Lý thuyết : 3 tiết Thực hành 12 tiết

2 Data Controls DataGrid =Table(động được tạo trên server) Repeater
DataList GridView

3 Các properties của Data Controls
DataList DataGrid Column Layout(sắp xếp theo cột) X Paging(Hiển thị số trang) Select/Edit/Delete Sort Style Properties(Kiểu cách) Table Layout(Hiển thị kiểu Bảng) Template(Sử dụng khuôn mẫu)

4 DataGrid Ý nghĩa: Cung cấp một dạng bảng tính dữ liệu từ data source
Gồm tập các cột, một header, một footer. Các cột có thể tự động tạo hoặc tường minh Thuộc tính: DataField ……. HeadText 03CDTH632 ……. 03CDTH892 Student ID <asp:BoundColumn DataField=“IDs” HeadText=“Student ID” />

5 Properties Descriptions AutoGenerateColumns Tạo column tự động() Column(Design & Coding) Bound Column Cột dữ liệu Button Column Cột dạng Button HyperLink Column Cột dạng Liên Kết Template Column Cột tự thiết kế EditItemStyle Định dạng cho Item Header Style(Show Footer: true) Định dạng Header Footer Style(Show Header: true) Định dạng Footer GridLines Hiển thị dạng lưới

6 Ví dụ: Tạo điều khiển (Design)
Ràng buộc vào DataReader/Dataset: (Coding) <asp:DataGrid id="dgAuthors" runat="server" /> dgAuthors.DataSource = ds.Table[0]; dgAuthors.DataBind(); dgAuthors.DataSource = dr; dgAuthors.DataBind();

7 DataGrid – Ví dụ <form id="form1" runat="server"> <div>
<asp:DataGrid ID="DataGrid1" runat="server" Style="z-index: 100; left: 55px; position: absolute;top: 15px"> </asp:DataGrid> </div> </form>

8 DataGrid Editing Sử dụng EditCommandColumn
Thuộc tính EditItemIndex (-1 ->không có dòng nào được hiệu chỉnh) EditCommandColumn hiển thị 3 liên kết tương ứng 3 sự kiện: OnEditCommand, OnUpdateCommand, OnCancelCommand The DataGrid provides a special EditCommandColumn designed for editing data DataGrid maintains an EditItemIndex property that indicates which row of the grid should be editable (-1 indicates no row is being edited) The EditCommandColumn renders links for firing three events: EditCommand, UpdateCommand, CancelCommand DataGridCommandEventArgs is passed into each handler, which contains information on the row being edited DataGrid also maintains a DataKeyField property that should be set to the primary key In the event handler for the UpdateCommand, you access the DataKeys collection of the DataGrid to populate the primary key value

9 DataGrid Editing DataGridCommandEventArgs chứa thông tin của dòng sẽ được hiệu chỉnh Thuộc tính DataKeyField thiết lập khóa chính Sự kiện UpdateCommand, truy xuất tập các DataKeys để thao tác trên khóa chính

10 GridView các sự kiện OnEditCommand=“FunctionEdit"
OnCancelCommand=“FunctionCancel“ OnPageIndexChanged=“FunctionPage" OnDeleteCommand=“FunctionDelete" OnItemCommand=“FunctionCommand"

11 Gán dữ liệu Access vào DataGrid
protected void Page_Load(object sender, EventArgs e) { String strCon = "Provider=Microsoft.Jet.OleDb.4.0;Data Source = "; strCon = strCon + Server.MapPath(".") + "\\App_Data\\SV.mdb"; OleDbConnection con = new OleDbConnection(strCon); con.Open(); String strOLE = "select * from tblSinhVien"; OleDbDataAdapter adap = new OleDbDataAdapter(strOLE, con); DataSet ds = new DataSet(); adap.Fill(ds); DataGrid1.DataSource = ds; //kết gán dl với GridView có id là GridView1 DataGrid1.DataBind(); } // lưu ý using System.Data.OleDb;

12 Gán dữ liệu SQL vào DataGrid
protected void Page_Load(object sender, EventArgs e) { string strconn = "server=TRUCLY-PC\\SQL2005;database=SINHVIEN;uid=sa;pwd=sa"; SqlConnection conn = new SqlConnection(strconn); string sql = "SELECT * FROM tblsinhvien"; SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataAdapter adap=new SqlDataAdapter(sql,conn); DataSet ds = new DataSet(); adap.Fill(ds); DataGrid1.DataSource = ds; //kết gán dl với DataGrid1 DataGrid1.DataBind(); }

13 Hiệu chỉnh dữ liệu - DataGrid

14 Hiệu chỉnh DataGrid – Mã lệnh
protected override void OnLoad(EventArgs e) { if (!IsPostBack) BindGrid(); base.OnLoad(e); } public void gd1_Edit(object src, DataGridCommandEventArgs e) { gd1.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); } public void gd1_Cancel(object src, DataGridCommandEventArgs e) { gd1.EditItemIndex = -1; }

15 Đặc điểm khác của DataGrid
Tùy biến giao diện GridView ButtonColumn: hiển thị nút nhấn nhằm thực thi một lệnh nào đó. Thuộc tính AllowSorting: hiển thị tiêu đề cột như một siêu liên kết. Thuộc tính SortField : chỉ ra field được sắp xếp.

16 PROPERTY DATAGRID

17 Định dạng Datagrid

18 Thực hành: Sử dụng DataGrid
Tạo một OleDbConnection Tạo một OleDbCommand Tạo a DataReader Kéo một DataGrid vào Web Form Ràng buộc DataSource vào DataGrid

19 Repeater Ý nghĩa: Templates:
Ràng buộc dữ liệu từ danh sách các item. Hiển thị dữ liệu dựa trên các phần tử HTML Templates: The Repeater control is a simple container control that binds to a list of items. It walks through the bound items and produces graphical elements according to a basic rendering algorithm and the HTML templates you supply. The Repeater control supports from one through five templates TemplateDescriptionHeaderTemplateDetermines the heading of the final output. It is rendered only once and prior to any row. It cannot contain data bound information.ItemTemplateDetermines the output format of each row in the data source. This template is called for each item in the list and can contain data binding expressions.AlternatingItemTemplateFunctions similarly to ItemTemplate. This template applies only to rows with an odd ordinal position. SeparatorTemplateDetermines the HTML content that goes between each row. It cannot contain data bound information.FooterTemplateRendered only once when all items have been rendered. It cannot contain data bound information.

20 DataBinder Phân tích và đánh giá biểu thức ràng buộc dữ liệu
Tính toán biểu thức ràng buộc dữ liệu để rút trích dữ liệu đúng trong template Container.DataItem trả về đối tượng Object DataBinder.Eval() là hàm tĩnh sử dụng Reflection Không quan tâm đến kiểu của nguồn dữ liệu (data source)

21 DataBinder – Ví dụ <asp:Repeater id="rp1" runat="server">
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Name") %><br> <%# DataBinder.Eval(Container.DataItem, "Age","{0:2d}") %> <br> </ItemTemplate> </asp:Repeater>

22 A Repeater control in use
When you call DataBind on the Repeater control, the control first attempts to locate the HeaderTemplate and, if found, applies it. The control then loops through the list items and applies the ItemTemplate to each one. When the AlternatingItemTemplate is defined, odd rows take it. If the SeparatorTemplate is found, the template is applied in between two consecutive items, regardless of whether the template item is normal or an alternate. When the FooterTemplate is specified, it is applied at the end of the loop.

23 DataList Sử dụng DataList hiển thị dữ liệu từ data source
Sử dụng khi hiển thị dữ liệu dạng danh sách (tương tự dạng Table) Đơn giản hơn DataGrid Ít nhất có ItemTemplate Mỗi template có kiểu dáng riêng (HeaderStyle và ItemStyle)

24 DataList – Ví dụ

25 Datalist

26 Ràng buộc dữ liệu từ Data Reader
Thiết lập thuộc tính Gọi phương thức DataBind Thuộc tính Mô tả DataSource Nguồn dữ liệu DataTextField Field trong DataTable được hiển thị DataValueField Field trong DataTable trở thành giá trị của item Id.DataSource = dr; dgEmp.DataBind();

27 Minh họa: Điều khiển DataGrid
Sử dụng AutoFormat Thiết lập Tiêu đề cột Phân trang Sắp xếp

28 Dataset là gì? DataSet OleDb Server 2000 OleDb Database DataTable
OleDbDataAdapter Bộ nhớ Thiết bị lưu trữ OleDbConnection OleDbDataAdapter OleDbConnection OleDb Server 2000 OleDb Database

29 Truy cập dữ liệu với ADO.NET
Database Yêu cầu từ Client 1 2 Tạo đối tượng kết nối OleDbConnection và OleDbDataAdapter Điền dữ liệu vào DataSet từ DataAdapter, đóng kết nối OleDbConnecti on 3 Web server Trả DataSet cho Client 4 OleDbDataAdap ter Client thao tác dữ liệu 5 6 Cập nhật DataSet Sử dụng OleDbDataAdapter để mở OleDbConnection, cập nhật cơ sở dữ liệu và đóng kết nối 7 DataSet List-Bound Control Client


Download ppt "CHƯƠNG VII: KẾT GÁN DỮ LIỆU"

Similar presentations


Ads by Google