2017-02-05 26 views
0

我有一个“搜索”按钮来搜索datagrid中的数据/单元格,这是源的MySQL数据库。下面的代码块是成功搜索只有一列,但是当我添加其他列比搜索功能不工作,并且大多不会带来结果。以及区分大小写的错误,这对于只有一列是不成问题的。C#Winform的数据网格错误搜索值

我该如何安排代码才能够搜索所有行和列?

private void btnSearch_Click(object sender, EventArgs e) 
     { 
      DataView DV = new DataView(dbdataset); 
      DV.RowFilter = string.Format("Name LIKE '%{0}%'", txtSearch.Text); 
      dgvEkip.DataSource = DV; 

      // I added those columns below for search function as well but did not work well 
      /* 
      DV.RowFilter = string.Format("Telephone LIKE '%{0}%'", txtSearch.Text); 
      DV.RowFilter = string.Format("Email LIKE '%{0}%'", txtSearch.Text); 
      DV.RowFilter = string.Format("Surname LIKE '%{0}%'", txtSearch.Text); 
      DV.RowFilter = string.Format("City LIKE '%{0}%'", txtSearch.Text); 
      DV.RowFilter = string.Format("Adress LIKE '%{0}%'", txtSearch.Text); 
      */ 
     } 

非常感谢,鹦鹉。

+0

你尝试使用OR? https://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.expression.aspx – KernelMode

回答

0

你想要放置更多的条件在逻辑OR:例如

DV.RowFilter = string.Format("Name LIKE '%{0}%' OR Telephone LIKE '%{0}%' OR Email LIKE '%{0}%' ", txtSearch.Text); 
+0

它工作得很好。谢谢 :) –

0

使用和加入这样的条件:

DV.RowFilter = string.Format("Telephone LIKE '%{0}%' AND Email LIKE '%{0}%'", txtSearch.Text, txtSearch2.Text); 
+0

OR和AND工作得很好。感谢您的帮助@McNets –