如何在范围之间过滤datagridview行?

时间:2017-01-29 18:19:01

标签: c# datagridview filter range between

我有一个名为拷贝号的专栏,其中只包含如下数字:

copy number
 1
 33
 12
 40
 100

如何在 1 100 之间获取行?此示例中的范围是行(33,12,40)?

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

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

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

1 个答案:

答案 0 :(得分: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