【问题标题】:How to filter datagridview rows between range? [closed]如何过滤范围之间的datagridview行? [关闭]
【发布时间】:2017-01-29 18:19:01
【问题描述】:

我有一个名为 copy number 的列,其中仅包含如下数字:

copy number
 1
 33
 12
 40
 100

我怎样才能得到 1100 之间的行,在这个例子中是行(33、12、40)?

我的数据来自 mysql 数据库,GridView 填充如下:

MySqlDataAdapter a = new MySqlDataAdapter(query, conn)
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;

PS:非常感谢代码中的答案!

【问题讨论】:

  • 您遗漏了大量信息:数据是如何进入控件的?是绑定的吗?你试过什么?
  • 如果您的 DGV 绑定到数据源,请在 DGV 与其数据源之间插入“BindingSource”,然后使用 BindingSource 的“Filter”属性。
  • 过滤字符串将包含什么...这就是我需要的?
  • 我假设你说的是 WinForms?也许看看这个article,它解释了在 Windows 窗体 DataGridView 控件中对数据进行排序。特别是 DataGridView.Sort method 上的页面,其中用 C# 示例进行了解释。

标签: c# datagridview filter range between


【解决方案1】:

您可以尝试挖掘文章"BindingSource.Filter Property" on MSDN

根据那里的信息,我会尝试对您的代码执行此操作:

// your original starter code
MySqlDataAdapter a = new MySqlDataAdapter(query, conn)
DataTable t = new DataTable();
a.Fill(t);

// Create a seperate bindingsource object you can control
var bindingSource = new BindingSource();
bindingSource.DataSource = t;

// Now sort on a column
bindingSource.Filter = "[copy number] >= 1 AND [copy number] <= 100";

// Assign that bindingsource object to the dgv
dataGridView1.DataSource = bindingSource;

您可能希望将列名 copy number 编辑为 cncopy_number,我在此处添加了括号 [],因为那是 the way you can overcome it

【讨论】:

  • 很抱歉让您失望了,但我不需要排序。我需要的是一种基于特定范围(最小值、最大值)获取行的方法。
  • 我的错 - 读得太快了..你看过BindingSource.Filter Property吗?我会尝试调整/编辑我上面的答案。
  • @Joseph - 忘了提 - 如果你想改变数字范围,你可以使用这样的东西:bindingSource.Filter = String.Format("[copy number] &gt;= {0} AND [copy number] &lt;= {1}", number1, number2); - 当然,number1number2 是你可以分配的变量通过滑块或任何其他用户输入?或者如果您使用的是 C# + 或更高版本,请使用 string interpolationbindingSource.Filter = $"[copy number] &gt;= {number1} AND [copy number] &lt;= {number2}";
  • 成功了!!!非常感谢 >。
猜你喜欢
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多