如何使用多行搜索过滤器来搜索ID和名称?

时间:2014-09-30 21:42:15

标签: c# datagrid filter textbox multiple-columns

我需要为我正在开发的系统制作一个类似的过滤器,但是我只能使用单行,但是当我尝试添加更多行并运行程序时它停止并给出错误,这是我正在使用的代码:

public partial class frmconsultamateriaprima : Form
{
    DataView view = new DataView();

    public frmconsultamateriaprima()
    {
        InitializeComponent();
    }

    private void frmconsultamateriaprima_Load(object sender, EventArgs e)
    {
        DataTable datatable = new DataTable();
        SqlConnection con = new SqlConnection(@"Data Source=USER-PC;Initial Catalog=dbpuntodeventa;Integrated Security=True");
        con.Open();
        datatable.Load(new SqlCommand("select * from materiaprima", con).ExecuteReader());
        dataGridView1.DataSource = view = datatable.DefaultView;
        con.Close();
    }

    private void txtfiltro_TextChanged(object sender, EventArgs e)
    {
        view.RowFilter = (" nombre like  '%" + txtfiltro.Text + "%' or id like  '%" + txtfiltro.Text + "%'");
        if (txtfiltro.Text == "")
        view.RowFilter = string.Empty;

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用CONVERT函数将id转换为字符串,如此...

private void txtfiltro_TextChanged(object sender, EventArgs e)
{
    view.RowFilter = " nombre like  '%" + txtfiltro.Text + 
          "%' or CONVERT(id,'System.String')  like  '%" + txtfiltro.Text + "%'";
}