如何使用C#通过两个文本框过滤DataGridView [英] how to filter the DataGridView by two textboxes using C#

查看:80
本文介绍了如何使用C#通过两个文本框过滤DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表格中,



我有Textbox1(带有label1 [来自大小])和Textbox2(带有label2 [To size。



我有一个按钮(搜索按钮)。



我有一个dataGridView1,它包含来自本地数据库的一个表的绑定源。 dgv中的一列是size列。



我的问题是:



如果我在textbox1中输入from size(ex:3.2英寸),在textbox2中输入To size(ex:3.5英寸)并按下搜索按钮如何在dataGridView中获取具有此大小范围的行???



我这样做是为了通过datetimepickers过滤dgv



In my form,

I have Textbox1(with label1 [From size]) and Textbox2(with label2 [To size.

I have a button(search button).

I have a dataGridView1 which includes binding source from one table form the local database. and one of the columns in the dgv is size column.

My question is:

if I enter the from size (ex: 3.2 inch) in textbox1 and To size (ex: 3.5 inch) in textbox2 and press on search button how can I get the rows the have this size range in the dataGridView ???

I did this to filter the dgv by datetimepickers

private void pictureBox10_Click(object sender, EventArgs e) 
{
 
        string From = Convert.ToDateTime(dateTimePicker1.Value).ToString("yyyy-MM-dd HH:mm:ss.ff");
        string To = Convert.ToDateTime(dateTimePicker2.Value).ToString("yyyy-MM-dd HH:mm:ss.ff");
        table1BindingSource.Filter = string.Format("([{0}] >= #{1}# AND [{0}] <= #{2}#)", "Date", From, To);
    }

推荐答案

这样的事情应该有效。与您现有的代码非常相似。

可能必须更改过滤器的确切语法。

Something like this should work. Very similar to your existing code.
The exact syntax of the filter might have to be changed.
private void buttonSearch_Click(object sender, EventArgs e)
{
    table1BindingSource.Filter = string.Format("([{0}] >= {1} AND [{0}] <= {2})", "Size", Textbox1.Text, Textbox2.Text);
    }



有关语法的帮助,请参阅 DataColumn.Expression属性 [ ^ ]


这篇关于如何使用C#通过两个文本框过滤DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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