語言

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性質。DataView 執行此範例時,將程式碼貼上到 Windows 表單,並呼叫 PopulateDataViewAndFilter 表單的建構子或 Load 事件處理方法。 你的表單應該會匯入 System.Xml 和 System.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 將此性質傳遞給底層清單。 如果你在物件初始化時設定此屬性,呼叫會延後直到初始化完成後。

要形成濾波器值,先指定欄位名稱,後接運算子和一個篩選用的值。 被接受的過濾語法取決於底層資料來源。 如果底層資料來源是 DataSet、 DataTable或 DataView,你可以使用該屬性所記錄 DataColumn.Expression 的語法來指定布林運算式。

房產的 Filter 價值會影響房產 Count 本身的價值。 此外,當資料來源變更時,該 Filter 值仍會持續存在。 要停止過濾 , DataSource呼叫 方法 RemoveFilter 。

適用於

另請參閱