DataSet and DataGridView Datasource binding

佐手、 提交于 2020-01-23 17:26:06

问题


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:

For example: (Where 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

//Show these results in the dgv without changing the initial binding.


回答1:


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;

Example using the Northwind database:

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


来源:https://stackoverflow.com/questions/9160271/dataset-and-datagridview-datasource-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!