有兩種方式,壹種是直接綁定到數據源對象,另壹種是綁定到BindingSource組件,將BindingSource的DataSource屬性設置為壹個列表,第二種方法更常用。
下例查詢SalesOrderHeader表中2001年8月8號以後的序列,使用CopyToDataTable方法從查詢產生DataTable。並綁定到BindingSource。
DataTable?orders?=?ds.Tables["SalesOrderHeader"];
//查詢SalesOrderHeader表中2001年8月以後的訂單
IEnumerable<DataRow>?query?=?from?order?in?orders.AsEnumerable()
where?order.Field<DateTime>("OrderDate")?>?new?DateTime(2001,?8,?1)
select?order;
//用CopyToDataTable方法從查詢生成表
DataTable?boundTable?=?query.CopyToDataTable<DataRow>();
//綁定表作為BindingSource的數據源對象
bindingSource1.DataSource?=?boundTable;
dataGridView1.AutoResizeColumns();
dataGridView1.DataSource?=?bindingSource1;