通过


BindingSource.Filter 属性

定义

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

public:
 virtual property System::String ^ Filter { System::String ^ get(); void set(System::String ^ value); };
public virtual string Filter { get; set; }
public virtual string? Filter { get; [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members of types used in the filter expression might be trimmed.")] set; }
public virtual string Filter { get; [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members of types used in the filter expression might be trimmed.")] set; }
member this.Filter : string with get, set
[<set: System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members of types used in the filter expression might be trimmed.")>]
member this.Filter : string with get, set
Public Overridable Property Filter As String

属性值

一个字符串,指定如何筛选行。 默认值为 null

实现

属性

示例

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

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'";
}
Private Sub PopulateDataViewAndFilter() 
    Dim set1 As New DataSet()
    
    ' Some xml data to populate the DataSet with.
    Dim musicXml As String = "<?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.
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)
    
    ' Get a DataView of the table contained in the dataset.
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))
    
    ' Create a DataGridView control and add it to the form.
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True
    Me.Controls.Add(datagridview1)
    
    ' Create a BindingSource and set its DataSource property to
    ' the DataView.
    Dim source1 As 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'"

End Sub

注解

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

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

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

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

适用于

另请参阅