C#如何在DataView中多个RowFilter

时间:2016-05-15 17:04:57

标签: c# rowfilter access

我想知道如何根据不同文本框或组合框中的多个用户输入过滤数据。它工作但只有最新的输入。我如何将它们链接在一起?

    private void text_Sname_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);                      
        dv.RowFilter = string.Format ("[Supplier Name] LIKE '%{0}%'", text_Sname.Text);
        dataGridView1.DataSource = dv;            
    }

    private void text_Pnumber_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = string.Format("[Part Number] LIKE '%{0}%'", text_Pnumber.Text);
        dataGridView1.DataSource = dv;
    }

    private void text_Dwgnumber_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = string.Format("[Drawing Number] LIKE '%{0}%'", text_Dwgnumber.Text);
        dataGridView1.DataSource = dv;
    }

任何答案都将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以将过滤器保存在如下变量中:

string m_currentFilter=string.Empty;
string m_partNumberFilter=string.Empty;
string m_drawingNumberFilter=string.Empty;
然后写一个像

这样的方法
 private void applyFilter()
{
    generateFilter(m_drawingNumberFilter);
    generateFilter(m_partNumberFilter);
}

private void generateFilter(string filter)
{
  if(!string.IsNullOrEmpty(filter))
  {
   if(string.IsNullOrEmpty(m_currentFilter))
    {
      m_currentFilter=filter;
    }else
    {
       m_currentFilter+= " AND " + filter;
    }
   }
 }

然后你会像这样使用它:

    private void text_Pnumber_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView(dt);
            m_partNumberFilter=string.Format("[Part Number] LIKE '%{0}%'", text_Pnumber.Text);
            applyFilter();
            dv.RowFilter = m_currentFilter;
            dataGridView1.DataSource = dv;
        }

private void text_Dwgnumber_TextChanged(object sender, EventArgs e)
    {
        DataView dv = new DataView(dt);
       m_drawingNumberFilter= string.Format("[Drawing Number] LIKE '%{0}%'", text_Dwgnumber.Text);
        applyFilter();
        dv.RowFilter = m_currentFilter;
        dataGridView1.DataSource = dv;
    }

这应该只是你如何做到这一点的灵感,它不适合在生产中使用: - )