BindingSource.Filter 属性

定义

获取或设置用于筛选查看哪些行的表达式。

C#
public virtual string Filter { get; set; }
C#
public virtual string? Filter { get; set; }

属性值

用于指定行的筛选方式的字符串。 默认值为 null

实现

示例

以下示例演示如何将 属性与 一 Filter 起使用 DataView。 若要运行此示例,请将代码粘贴到 Windows 窗体中,然后从窗体的构造函数或Load事件处理方法调用 PopulateDataViewAndFilter 。 窗体应导入 System.XmlSystem.IO 命名空间。

C#
private void PopulateDataViewAndFilter()
{
    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string musicXml =
        "<?xml version='1.0' encoding='UTF-8'?>" +
        "<music>" +
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
        "</music>";

    // Read the xml.
    StringReader reader = new StringReader(musicXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.
    DataGridView datagridview1 = new DataGridView();
    datagridview1.AutoGenerateColumns = true;
    this.Controls.Add(datagridview1);

    // Create a BindingSource and set its DataSource property to
    // the DataView.
    BindingSource source1 = new BindingSource();
    source1.DataSource = view1;

    // Set the data source for the DataGridView.
    datagridview1.DataSource = source1;

    //The Filter string can include Boolean expressions.
    source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'";
}

注解

通常用于复杂的数据绑定方案, Filter 属性允许查看 的 DataSource子集。 只有实现 IBindingListView 接口的基础列表支持筛选。

当 不是 nullFilter,会将BindingSource此属性传递给基础列表。 如果在对象初始化期间设置此属性,则调用将推迟到初始化完成后。

若要形成筛选值,请指定列的名称,后跟运算符和要筛选的值。 接受的筛选器语法取决于基础数据源。 如果基础数据源是 DataSetDataTableDataView,则可以使用为 DataColumn.Expression 属性记录的语法指定布尔表达式。

属性的值 Filter 会影响属性的值 Count 。 此外,当数据源更改时, Filter 该值将保留。 若要停止筛选 , DataSource请调用 RemoveFilter 方法。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅