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

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

问题描述

我已将DataGridView与DataSet连接起来,其中包含我的一个数据库表中的所有值。但是我想要做的是过滤我的DataGridView以显示我的DataSet中的某些值:



例如:
(Where EmployeeID = 4)



有没有办法这样做而不改变我的初始绑定对象?

  //初始数据源
dgv.DataSource = DataSet1.Table [0];

//某些过滤器代码在此显示DataSet1其中employeeID = 1

//在dgv中显示这些结果,而不改变初始绑定。

解决方案

您可以使用 DataTable进行过滤和排序.DefaultView

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

使用Northwind数据库的示例:



<$ p从产品中选择ProductID,ProductName,SupplierID,CategoryID,UnitPrice; $ p>


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.

解决方案

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;

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

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