在datagridview中搜索数字

时间:2016-02-05 12:35:12

标签: c# datagridview

我试图在datagridview中搜索一个数字。 当我尝试搜索文本时,它完美无缺。但是,当我尝试搜索数字时,它不起作用。在我的专栏中,我的数字如下:1,2,22,33。所以,当我在文本框中键入:2时,我想在datagridview中看到数字2和22。

这是我的代码:

private void txtSearch_TextChanged(object sender, EventArgs e)
    {

        DataView dv = new DataView(tablecourse);
        string value = "CCarga LIKE %{0}%"; //Here is the error

        dv.RowFilter = string.Format(value, txtSearch.Text);
        dataGridView1.DataSource = dv; 
    }

错误:在MOD运算符之前缺少运算符。

当我尝试时:字符串值=“CCarga LIKE'%{0}%'”;

我得到:无法执行'Like'em System.UInt32 e System.String。

更新:这是更改。

value = "CONVERT(CCarga, System.String) LIKE '%{0}%'";

1 个答案:

答案 0 :(得分:2)

 string value = "CCarga LIKE '%{0}%'"; //Here is the error

在%

之后和之前添加单引号

在添加到问题的int类型后更新

然后将其转换为字符串,如下所示,您需要提供.NET类型,

  value = "CONVERT(CCarga, System.String) LIKE '%{0}%'";  ;

你应该通过.net fx引用usage of data view filter,而不是sql server。