Đẩy dữ liệu vào điều khiển lưới DataGrid

Một phần của tài liệu Phát triển ứng dụng cơ sở dữ liệu với c và net framework (Trang 97 - 99)

Chương 8 Ứng dụng Windows với WindowsForm

9.8.1. Đẩy dữ liệu vào điều khiển lưới DataGrid

Ví dụ sau sẽ dùng điều khiển lưới DataGrid để thực hiện kết buộc dữ liệu, điều khiển lưới này được hỗ trợ cho cả ứng dụng Windows Forms và WebForms.

Trong ứng dụng trước, ta phải duyệt qua từng dòng của đối tượng DataTable

để lấy dữ liệu, sau đó hiển thị chúng lên điều khiển ListBox. Trong ứng dụng này công việc hiển thị dữ liệu lên điều khiển được thực hiện đơn giản hơn, ta chỉ cần lấy về đối tượng DataView của DataSet, sau đó gán DataView này cho thuộc tính DataSource của điều khiển lưới, sau đó gọi hàm DataBind() thì tự động dữ liệu sẽ được đẩy lên điều khiển lưới dữ liệu.

CustomerDataGrid.DataSource =

DataSet.Tables["Customers"].DefaultView;

Trước tiên ta cần tạo ra đối tượng lưới trên Form bằng cách kéo thả, đặt tên lại cho điều khiển lưới là CustomerDataGrid. Sau đây là mã hoàn chỉnh của ứng dụng kết buộc dữ liệu cho điều khiển lưới :

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace ProgrammingCSharpWindows.Form {

public class ADOForm3: System.Windows.Forms.Form {

private System.ComponentModel.Container components; private System.Windows.Forms.DataGrid CustomerDataGrid; public ADOForm3( )

{

InitializeComponent( );

// khởi tạo chuỗi kết nối và chuỗi truy vấn dữ liệu string connectionString =

"server=localComputer; uid=sa; pwd=;database=northwind"; string commandString =

+ "Phone, Fax from Customers";

// tạo ra một SqlDataAdapter và DataSet mới, // đẩy dữ liệu cho DataSet

SqlDataAdapter DataAdapter =

new SqlDataAdapter(commandString, connectionString);

DataSet DataSet = new DataSet( ); DataAdapter.Fill(DataSet,"Customers"); // kết buộc dữ liệu của DataSet cho lưới

CustomerDataGrid.DataSource = DataSet.Tables["Customers"].DefaultView; }

public override void Dispose( ) {

base.Dispose( );

components.Dispose( ); }

private void InitializeComponent( ) {

this.components = new System.ComponentModel.Container(); this.CustomerDataGrid = new DataGrid();

CustomerDataGrid.BeginInit(); CustomerDataGrid.Location = new System.Drawing.Point (8, 24);

CustomerDataGrid.Size = new System.Drawing.Size (656, 224); CustomerDataGrid.DataMember = "";

CustomerDataGrid.TabIndex = 0; CustomerDataGrid.Navigate +=

new NavigateEventHandler(this.dataGrid1_Navigate); this.Text = "ADOFrm3";

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size (672, 273); this.Controls.Add (this.CustomerDataGrid);

CustomerDataGrid.EndInit ( ); }

public static void Main(string[] args) {

Application.Run(new ADOForm3()); }

} }

Điều khiển lưới sẽ hiển thị y hệt mọi dữ liệu hiện có trong bảng Customers, tên của các cột trên lưới cũng chính là tên của các cột trong bản dữ liệu. Giao diện của ứng dụng sau khi chạy chương trình:

Hình 9-5 Kết buộc dữ liệu cho điều khiển lưới DataGrid

Một phần của tài liệu Phát triển ứng dụng cơ sở dữ liệu với c và net framework (Trang 97 - 99)