我有一个“搜索”按钮,用于搜索datagrid中的数据/单元,这是源mysql db。下面的代码块可以成功地仅搜索一列,但是当我添加其他列而不是搜索功能时效果不佳,并且大多数情况下不会带来结果。同样,对于区分大小写的错误也仅适用于一列,这并不是一个问题。
如何安排代码以搜索所有行和列?
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);
*/
}
非常感谢Nuri。
最佳答案
您想在逻辑或中放置更多条件:例如
DV.RowFilter = string.Format("Name LIKE '%{0}%' OR Telephone LIKE '%{0}%' OR Email LIKE '%{0}%' ", txtSearch.Text);