2017-10-19 59 views
-1

我使用的datagridview綁定到數據表中顯示我的記錄,我有這樣的(只是舉例)記錄
名稱國家
過濾器的datagridview使用數據視圖

JAck  Country1 City1 
Name2 Country1 City2 
JACK  Country2 City1 
多個關鍵字

我有文本框搜索我不想使用這個文本框在這個datagridview中搜索多個關鍵字 我想例如得到Country1的所有記錄,並有名字插孔 我把一個字符串文本框這樣
[JACK國家1]
我創建這個方法基於任何列

string query = ""; 
     Boolean firsttime = true; 
     bool firstkeyword = true; 
     foreach (string se in txt_ar_recherche.Text.Trim().Split(' ')) 
     { 
      string search = (Convert.ToString(" like '%") + se) + "%'"; 
      if(!firstkeyword) query += " and "; 

      query += "("; 
      bool firstcolumn = true; 

      foreach (DataGridViewColumn col in grid.Columns) 
      { 
       if (col.Visible) 
       { 
        if (firsttime) 
        { 
         query += "Convert(" + col.Name + ",'System.String')" + search; 
         firsttime = false; 
        } 
        else 
        { 
         query += " or " + "Convert(" + col.Name + ",'System.String')" + search; 
        } 
       } 
       firstcolumn = false; 
      } 
      firstkeyword = false; 
      query += ")"; 

     } 
     (grid.DataSource as DataTable).DefaultView.RowFilter = query; 

創建動態的RowFilter查詢,但它不工作,我不知道如何解決這個問題在datagridview的 我wan't創建的部份相同的文本框搜索該數據表使用JavaScript mutilple關鍵字搜索,這是一個演示https://datatables.net/

+0

如何生成查詢看起來像?什麼不行?崩潰的應用程序?在某些列上進行錯誤的過濾? – Reniuz

+0

做了一些測試,如果你看看你的查詢..你會發現一個不相關的'或'可能會導致問題 – Reniuz

+0

但沒有或查詢這樣的loke(名稱像'%JACK%'或國家像'%JACK%'或像'%JACK%'這樣的城市)和(像'%country1%'這樣的名字或像'%country1%'這樣的國家或像'%country1%'這樣的城市)它不起作用 –

回答

1

試試這個全樣本:

using System; 
using System.Data; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApp1 
{ 
    public partial class Form1 : Form 
    { 
     DataGridView dataGridView; 
     DataTable dataTable; 
     DataView dataView; 
     TextBox textBoxSearch; 

     public Form1() 
     { 
      //InitializeComponent(); 

      Width = 800; 
      dataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top }; 
      textBoxSearch = new TextBox { Parent = this, Top = 200 }; 
      textBoxSearch.TextChanged += TextBoxSearch_TextChanged; 

      dataTable = new DataTable(); 

      dataTable.Columns.Add("Name"); 
      dataTable.Columns.Add("Country"); 
      dataTable.Columns.Add("City"); 

      dataTable.Rows.Add("JAck", "Country1", "City1"); 
      dataTable.Rows.Add("Name2", "Country1", "City2"); 
      dataTable.Rows.Add("JACK", "Country2", "City1"); 

      dataView = new DataView(dataTable); 
      dataGridView.DataSource = dataView; 
     } 

     private void TextBoxSearch_TextChanged(object sender, EventArgs e) 
     { 
      var words = textBoxSearch.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 

      if (!words.Any()) 
      { 
       dataGridView.DataSource = dataView; 
       return; 
      } 

      var dv = dataView; 

      foreach (var word in words) 
      { 
       var values = dataTable.Columns 
        .OfType<DataColumn>() 
        .Select(c => "Convert([" + c.ColumnName + "], System.String)") 
        .Select(c => c + " like '%" + word + "%'"); 

       var filter = string.Join(" or ", values); 
       dv = new DataView(dv.ToTable()); 
       dv.RowFilter = filter; 
       dataGridView.DataSource = dv; 
      } 
     } 
    } 
} 
+0

非常感謝你 –