使用组合框和文本框搜索datagridview [英] Search datagridview by using combobox and textbox

查看:110
本文介绍了使用组合框和文本框搜索datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时,数据库没有改变 - 当我向文本框中添加文本以进行搜索时没有任何反应

When run, the database has not changed- nothing happens when I add text to the textbox to search with

private void textBox5_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (String.IsNullOrEmpty(textBox5.Text))
            customer_OrdersBindingSource.Filter = string.Empty;
        else
            customer_OrdersBindingSource.Filter = string.Format("{0}='{1}'", comboBox1.Text, textBox5.Text);
    }
}





我尝试了什么: < br $>




What I have tried:

private void textBox5_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (String.IsNullOrEmpty(textBox5.Text))
            customer_OrdersBindingSource.Filter = string.Empty;
        else
            customer_OrdersBindingSource.Filter = string.Format("{0}='{1}'", comboBox1.Text, textBox5.Text);
    }
}

推荐答案

一个显而易见的问题是,当表达式如何表达时它被添加到过滤器中。使用调试器,在设置过滤器的行上放置一个断点,并确保过滤器具有适当的值。例如,组合框中的文本与数据源中列的命名方式完全相同



清除过滤器时,而不是将空字符串设置为过滤器,建议使用 BindingSource.RemoveFilter方法(System.Windows.Forms) [ ^ ]



我认为这个想法是允许用户搜索部分文本,换句话说,文本不需要与整个数据源中的值匹配。如果是这种情况,您可能应该将相等比较更改为LIKE。换句话说,比如

One obvious question is, how does the expression look like when it's added into the filter. using debugger, place a breakpoint on the line where you set the filter and ensure that the filter has proper value. For example is the text in combobox written exactly the same way how the column is named in the data source

When clearing the filter, instead of setting an empty string to the filter, it's recommended to use BindingSource.RemoveFilter Method (System.Windows.Forms)[^]

I take it that the idea is to allow user to search with partial text, in other words the text does not need to match the value in the data source in whole. If this is the case you probably should change the equality comparison to LIKE. In other words something like
customer_OrdersBindingSource.Filter = string.Format("{0} LIKE '{1}*'", comboBox1.Text, textBox5.Text);

有关详细信息,请参阅 DataColumn.Expression属性(系统.Data) [ ^ ]

For more information, see DataColumn.Expression Property (System.Data)[^]


这篇关于使用组合框和文本框搜索datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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