【问题标题】:Using a textbox entry to filter a datagridview with an int value? VS C#使用文本框条目过滤具有 int 值的 datagridview?与 C#
【发布时间】:2016-08-29 13:42:59
【问题描述】:

我使用 datagridview 来显示数据,这段代码使用文本框搜索/过滤它。

private void textBox1_TextChanged(object sender, EventArgs e)
   {

       try
       {

           BindingSource bs_sp = new BindingSource();

           bs_sp.DataSource = senior_playersDataGridView.DataSource;

           bs_sp.Filter = "first_name like '%" + textBox1.Text + "%'";

           senior_playersDataGridView.DataSource = bs_sp;
       }
       catch (System.Exception ex)
       {
           MessageBox.Show("Search failed");  
       }
   }

这成功地通过用户输入的名字搜索/过滤数据库,这是一个字符串。

但是,如果我想搜索/过滤使用 int 数据类型的列,显然 like 语句不再有效,因为这只适用于字符串。

如何修改它以使其将数字输入到文本框中,并按该 int 值搜索网格?

【问题讨论】:

  • From here: bs.Filter = "CONVERT(dat, 'System.String') LIKE '98%'";

标签: c# database visual-studio datagridview filter


【解决方案1】:

你可以使用转换。

bs_sp.Filter = "Convert(age, 'System.String') like '%" + textBox1.Text + "%'";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多