共用方式為


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; set; }
member this.Filter : string with get, set
Public Overridable Property Filter As String

屬性值

字串,指定資料列的篩選方式。 預設為 null

實作

範例

下列範例示範如何搭配 DataView 使用 Filter 屬性。 若要執行此範例,請將程式碼貼到 Windows Form 中,然後從表單的建構函式或 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 介面的基礎清單支援篩選。

當 不是 nullFilter ,會將 BindingSource 此屬性傳遞至基礎清單。 如果您在物件初始化期間設定此屬性,則會延遲呼叫,直到初始化完成為止。

若要形成篩選值,請指定要篩選的資料行名稱,後面接著運算子和要篩選的值。 接受的篩選語法取決於基礎資料來源。 如果基礎資料來源是 DataSetDataTableDataView ,您可以使用針對 屬性記載的 DataColumn.Expression 語法來指定布林運算式。

屬性的值 Filter 會影響 屬性的值 Count 。 此外,當資料來源變更時, Filter 值會保存。 若要停止篩選 DataSource ,請呼叫 RemoveFilter 方法。

適用於

另請參閱