【问题标题】:How can I make a search filter with multiple rows to search from like id and name?如何制作一个包含多行的搜索过滤器,以便从 id 和 name 中进行搜索?
【发布时间】:2014-11-25 15:47:48
【问题描述】:

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

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;

    }
}

【问题讨论】:

  • 你能不能用单引号试试下面的东西,例如view.Select("nombre like '" & txtfiltro.Text & "%'")等。遵循相同的id模式......可能重复,但请查看此帖子how I can sear row
  • 虽然@DJKRAZE 可能是对的,但能否请您发布您的完整错误?
  • 我尝试了德雷克所说的,但我不断收到错误消息,它说类似的部分不能用于 system.int32 和 system.string
  • id 必须是 Int32,不能使用 LIKE
  • 那么我应该用什么代替like呢?

标签: c# datagrid filter textbox multiple-columns


【解决方案1】:

您可以像这样使用CONVERT 函数将 id 转换为字符串...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多