Datagridview行筛选器

本文关键字:筛选 Datagridview | 更新日期: 2023-09-27 18:29:01

我正在使用DataViewRowFilter。只有一列在过滤,但我希望所有列都经过筛选。

我想在文本框中搜索一个单词来制作这个过滤器。

DataView dv = dt.DefaultView;
if (e.KeyChar == (char)13)
{
    dv.RowFilter = string.Format("Name LIKE '%{0}%'" ,textBox1.Text );
    //  dv.RowFilter = string.Format("Date LIKE '%{0}%'", textBox1.Text);
}

列标题name = NameDate等。

Datagridview行筛选器

对于多列,格式应该是这样的;

dv.RowFilter = "Column1 = " + value1 + " AND Column2 = " + value2;

根据您的场景;

dv.RowFilter = string.Format("Name LIKE '%{0}%' AND Date = {1}" ,textBox1.Text, DateValue );
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name = '{0}'", TextBoxForSearch.Text);
string query = textBox1.Text;
if(query == string.Empty)
{
    dataTable.DefaultView.RowFilter = string.Empty;
}
else
{
    (dgv.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name = {0}", textBox1.Text);
    (dgv.DataSource as DataTable).DefaultView.RowFilter = string.Format("Date = {1}", textBox1.Text);
}