2016-03-02 40 views
0

例如,当姓氏相同的名字 过滤姓名并在姓氏过滤器中写入,但保留姓名过滤器。如何在两个文本框中使用dataGridView实现多重过滤?

例蒙山一个文本框:

连接:

public void read_data(string query, ref DataSet principal, string tabla) 
{ 
    try 
    { 
     string cadena = "Server=0.0.0.0; Database=DB; Uid=user; Pwd=pass"; 
     MySqlConnection cn = new MySqlConnection(cadena); 
     MySqlCommand cmd = new MySqlCommand(query, cn); 
     cn.Open(); 
     MySqlDataAdapter da = new MySqlDataAdapter(cmd); 
     da.Fill(principal, tabla); 
     cn.Close(); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

负载的dataGridView:

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.read_data("Select * From reg", ref result, "reg"); 
    this.filtro = ((DataTable)result.Tables["regi"]).DefaultView; 
    this.dataGridView1.DataSource = filtro; 
} 

事件:

private void textBox1_KeyUp(object sender, KeyEventArgs e) 
{ 
    string data_output = " "; 
    string[] search_word = this.textBox1.Text.Split(' '); 

    foreach (string word in search_word) 
    { 
     if (data_output.Length == 0) 
     { 
      data_output = "(FirstName LIKE '%" + word + "%')"; 
     } 
     else 
     { 
      data_output += "(FirstName LIKE '%" + word + "%')"; 
     } 
    } 
    this.filtro.RowFilter = data_output; 
} 
+0

任何过滤器表达式请问您提供范例作品?即当您指定多个由空格分隔的名字时它是否筛选记录? – fujiFX

+0

不,我没有。只有名字 – Ale

+0

无论如何,请检查我刚刚提供的答案,看看它是否有助于您实现目标。 – fujiFX

回答

1

这里有一个方法来实现你的requiremen吨。

创建常用的功能,从两个文本框调用如下

private string GetFilterExpression(string sFilterExprValue, string sFilterFieldName) 
    { 
     string sFilterExpr = ""; 
     string[] search_word = sFilterExprValue.Split(' '); 

     foreach (string word in search_word) 
     { 
      if (sFilterExpr.Length == 0) 
      { 
       sFilterExpr = "(" + sFilterFieldName + " LIKE '%" + word + "%')"; 
      } 
      else 
      { 
       sFilterExpr += " AND (" + sFilterFieldName + " LIKE '%" + word + "%')"; 
      } 
     } 

     return sFilterExpr; 
    } 

呼叫从两个文本框,如下图所示键向上事件的共同作用;

文本框1名过滤

private void textBox1_KeyUp(object sender, KeyEventArgs e) 
    { 
     // Text box to enter Firstname filter 
     string sFilter = GetFilterExpression(this.textBox1.text, "FirstName"); 

     if (!this.textBox2.text) 
     { 
      string sLastNameFilter = GetFilterExpression(this.textBox2.text, "LastName"); 

      if (!String.IsNullOrEmpty(sLastNameFilter))  // Concat the Last Name Filter 
       sFilter += String.IsNullOrEmpty(sFilter) ? "" : " AND " + sLastNameFilter; 
     } 

     this.filtro.RowFilter = sFilter; 
    } 

文本框2姓过滤

private void textBox2_KeyUp(object sender, KeyEventArgs e) 
    { 
     // Text box to enter Lastname filter 
     // Assume the field storing last name is 'Lastname' 
     string sFilter = GetFilterExpression(this.textBox2.text, "LastName"); 

     if (!this.textBox1.text) 
     { 
      string sFirstNameFilter = GetFilterExpression(this.textBox1.text, "FirstName"); 

      if (!String.IsNullOrEmpty(sFirstNameFilter)) 
       sFilter += String.IsNullOrEmpty(sFilter) ? "" : " AND " + sFirstNameFilter; 
     } 

     this.filtro.RowFilter = sFilter; 
    } 

希望这有助于并将其标记为因此,如果这个解决您的问题。

编辑:更新textBox2_Keyup事件来连接输入到textBox1的

+0

当我输入textbox1时,如果过滤,但在文本框2中写入,再次循环遍历所有记录,而不保留先前的过滤器。 – Ale

+0

根据你的评论,我认为你使用了建议的代码片段。而在代码中,'textbox2_keyup'事件中存在拼写错误,并且会覆盖'textbox1'过滤器。检查编辑的答案。 – fujiFX

+0

它现在可以工作,但我对程序做了一些更改,我用三个文本框进行了动态过滤。 我使用了你在其他程序中提供给我的代码,谢谢 – Ale

相关问题