2012-02-06 49 views
1

我已將DataGridView與DataSet與來自其中一個數據庫表的所有值鏈接起來。但我想要做的是篩選我的DataGridView顯示從我的數據集的某些值:DataSet和DataGridView數據源綁定

例如: (Where EmployeeID = 4)

有沒有這樣做而不會改變我的初始綁定對象的方法嗎?

//Initial datasource 
dgv.DataSource = DataSet1.Table[0]; 

//Some filter code here to display DataSet1 where employeeID = 1 

//在不改變初始綁定的情況下在dgv中顯示這些結果。

+0

我發現回答這個問題 使用的BindingSource對象 BindingSource的BS =新的BindingSource(); – SpaceApple 2012-02-06 13:59:11

回答

1

您可以使用DataTable.DefaultView進行過濾和排序。

DataTable dt = GetProductTable(); 
dt.DefaultView.Sort = "ProductName"; 
dt.DefaultView.RowFilter = "CategoryID=1"; 
dataGridView1.DataSource = dt.DefaultView; 

實施例使用Northwind數據庫:

select ProductID, ProductName, SupplierID, CategoryID, UnitPrice from Products; 
+0

感謝您的幫助!有用 – SpaceApple 2012-02-06 14:50:23