Tài liệu Data Binding doc

3 221 1
Tài liệu Data Binding doc

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

Thông tin tài liệu

Một số thao tác với Data Binding Author : Xcross87 Data Binding là cách mapping các thành phần của một data source vào một thành phần GUI và tự động làm việc với dữ liệu. Ví dụ có thể bind một cột (col) vào một TextBox qua thuộc tính Text hoặc có thể bind cả một table vào DataGrid như DataGridView. Có 2 cách bìn WinForm control vào dữ liệu : • Simple • Complex Simple Data Binding Là cách liên kết một-một giữa một thuộc tính của control và một thành phần của data source, và sử dụng control để hiển thị duy nhất một giá trị một lần. Thử một ví dụ : Tạo Winform App project, tại Form1 bạn cho thêm 2 textbox vào. Sau đó trong sự kiện : Form1_Load bạn chèn thêm đoạn code sau : [code] private void Form1_Load(object sender, EventArgs e) { string connString = @"Server = .\SQLEXPRESS; Integrated Security = true; Database = Northwind"; string sql = @"SELECT * FROM employees "; SqlConnection conn = new SqlConnection(connString); SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, "employees"); textBox1.DataBindings.Add("text", ds, "employees.firstname"); textBox2.DataBindings.Add("text", ds, "employees.lastname"); } [/code] Sau đó run thì thấy textbox1 là giá trị firstname đầu tiên trong record và textbox là giá trị lastname tương ứng. Complex Data Binding Là liên kết một control với một hoặc nhiều thành phần data của data source, có thể hiển thị nhiều hơn 1 giá trị một lần. Tạo một WinForm App project. Thêm vào một DataGridView, sau đó hiện ra DataGridView Task chọn Choose Data Source -> Add Project -> chọn Database -> New Connection -> SQL Server, Northwind Database -> Next đến khi nào thấy mục chọn Table cho DataSet thì chọn table : Customers rồi Finish Build -> Ctrl + F5 xem kết quả thu được Page 1 of 3 Một số thao tác với Data Binding Author : Xcross87 Binding Manager Class Tạo một WinForm App project. Đặt vào 2 textbox và 2 button Trong phần code của Form1.cs bạn chèn code sau : [code] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms; namespace SQLServerManger { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Tạo BindingManager private BindingManagerBase bMgr; // Sự kiện Form1_Load private void Form1_Load(object sender, EventArgs e) { // Tạo connection string string connString = @"Server = .\SQLEXPRESS; Integrated Security = true; Database = Northwind"; // Tạo Sql Query string sql = @"SELECT * FROM employees "; // Tạo connectioon SqlConnection conn = new SqlConnection(connString); // Tạo Adapter SqlDataAdapter da = new SqlDataAdapter(sql, conn); // Tạo DataSet DataSet ds = new DataSet(); // Lấp đầy DataSet da.Fill(ds, "employees"); // Bind giá trị cột firstname vào textbox1 textBox1.DataBindings.Add("text", ds, "employees.firstname"); // Bind giá trị cột lastname vào textbox2 textBox2.DataBindings.Add("text", ds, "employees.lastname"); // Cài Binding Manager vào DataSet để quản lý bMgr = this.BindingContext[ds, "employees"]; } Page 2 of 3 Một số thao tác với Data Binding Author : Xcross87 // Lấy record tiếp theo private void button2_Click(object sender, EventArgs e) { bMgr.Position += 1; } // Trở lại record trước private void button1_Click(object sender, EventArgs e) { bMgr.Position -= 1; } } } [/code] Phần này mình chằng có gì biết nhiều hết chỉ thế thôi. Đừng nghĩ là Data Binding dễ nhé, phức tạp lắm đó. Chẳng qua là code của Data Binding được Generate tự dộng nên mình có thể làm thao tác đơn giản thế này. Nhưng nếu thế này thì cũng không hay lắm vì không hiểu cách thức làm việc của nó. Tóm lại là sao cũng được. Kết thúc về Data Binding. Page 3 of 3 . với Data Binding Author : Xcross87 Data Binding là cách mapping các thành phần của một data source vào một thành phần GUI và tự động làm việc với dữ liệu. . SqlConnection(connString); // Tạo Adapter SqlDataAdapter da = new SqlDataAdapter(sql, conn); // Tạo DataSet DataSet ds = new DataSet(); // Lấp đầy DataSet da.Fill(ds, "employees");

Ngày đăng: 27/01/2014, 03:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan