DataSet 和 DataGridView 数据源绑定 [英] DataSet and DataGridView Datasource binding

查看:24
本文介绍了DataSet 和 DataGridView 数据源绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的 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;

这篇关于DataSet 和 DataGridView 数据源绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆