【问题标题】:DataSet and DataGridView Datasource bindingDataSet 和 DataGridView 数据源绑定
【发布时间】:2012-02-27 22:29:50
【问题描述】:

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

例如: (Where EmployeeID = 4),

有没有办法在不改变我的初始绑定对象的情况下做到这一点?

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

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

//在不改变初始绑定的情况下在dgv中显示这些结果。

【问题讨论】:

  • 我找到了这个问题的答案 Using a BindingSource Object BindingSource bs = new BindingSource();

标签: datagridview dataset


【解决方案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;

【讨论】:

  • 感谢您的帮助!它有效
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 2020-09-13
  • 2014-09-15
  • 2019-03-14
相关资源
最近更新 更多