In RowFilter, how To Select the all the Table Columns in C#?
Question
Here Is My Code...
Here i have one TextBox. When writing some text automatically match the String and find all Row..
now ..but in my code..here Only Filter using "ClientName" i want search that text from all the Column..
How to specify the All The Column in the RowFilter plz give me solution
private void txtSearch_TextChanged(object sender, EventArgs e)
{
dv.RowFilter = "ClientName Like '%" + txtSearch.Text + "%'";
dgClientMaster.DataSource = dv;
}
Solution
Try this code:
private void txtSearch_TextChanged(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (DataColumn column in dv.Table.Columns)
{
sb.AppendFormat("{0} Like '%{1}%' OR ", column.ColumnName, txtSearch.Text);
}
sb.Remove(sb.Length - 3, 3);
dv.RowFilter = sb.ToString();
dgClientMaster.DataSource = dv;
}
OTHER TIPS
You'll need to specify the columns you want to search, and separate with an OR
clause, like this:
dv.RowFilter = "ClientName Like '%" + txtSearch.Text + "%' OR ClientNickName Like '%" + txtSearch.Text + "%'";
Also, you'll need to escape single quotes, or you will get a broken query or SQL injection issues.
You can try
dv.RowFilter = "Column1 + Column2 + Column3" + " Like '%" + txtSearch.Text + "%'";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow