我已将我的 DataGridView 与一个包含我的一个数据库表中的所有值的 DataSet 链接起来.但我想要做的是过滤我的 DataGridView 以显示我的 DataSet 中的某些值:
I have linked my DataGridView with a DataSet with all the values from one of my database tables. But what i want to do is filter my DataGridView to display certain values from my DataSet:
例如:(其中 EmployeeID = 4)
,
有没有办法在不改变我的初始绑定对象的情况下做到这一点?
is there any way of doing this without changing my initial binding object?
//Initial datasource
dgv.DataSource = DataSet1.Table[0];
//Some filter code here to display DataSet1 where employeeID = 1
//在不改变初始绑定的情况下在dgv中显示这些结果.
//Show these results in the dgv without changing the initial binding.
您可以使用DataTable.DefaultView进行过滤和排序.
You can filter and sort using the DataTable.DefaultView.
DataTable dt = GetProductTable( );
dt.DefaultView.Sort = "ProductName";
dt.DefaultView.RowFilter = "CategoryID=1";
dataGridView1.DataSource = dt.DefaultView;
使用 Northwind 数据库的示例:
Example using the Northwind database:
select ProductID, ProductName, SupplierID, CategoryID, UnitPrice from Products;