Export (0) Print
Expand All

BindingSource.Filter Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets the expression used to filter which rows are viewed.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual string Filter { get; set; }
/** @property */
public String get_Filter ()

/** @property */
public void set_Filter (String value)

public function get Filter () : String

public function set Filter (value : String)

Property Value

A string that specifies how rows are to be filtered. The default is a null reference (Nothing in Visual Basic).

Typically used in complex data-binding scenarios, the Filter property allows you to view a subset of the DataSource. Only underlying lists that implement the IBindingListView interface support filtering.

When Filter is not a null reference (Nothing in Visual Basic), the BindingSource passes this property to the underlying list. If you set this property during object initialization, the call will be deferred until after initialization is complete.

To form a filter value, specify the name of a column followed by an operator and a value to filter on. The filter value must be inside quotation marks, such as "LastName = 'Smith'".

The value of the Filter property will persist when the data source changes. To stop filtering the DataSource, call the RemoveFilter method.

The following code example demonstrates the Filter and the Sort properties. To run this example, paste the code into a form that contains a BindingSource named BindingSource1 and a DataGridView named dataGridView1. Handle the form's load event and call InitializeSortedFilteredBindingSource in the load event-handling method. Visual Basic users will need to add a reference to System.Data.dll.

private void InitializeSortedFilteredBindingSource()
{
    // Create the connection string, data adapter and data table.
    SqlConnection connectionString =
         new SqlConnection("Initial Catalog=Northwind;" +
         "Data Source=localhost;Integrated Security=SSPI;");
    SqlDataAdapter customersTableAdapter =
        new SqlDataAdapter("Select * from Customers", connectionString);
    DataTable customerTable = new DataTable();

    // Fill the the adapter with the contents of the customer table.
    customersTableAdapter.Fill(customerTable);

    // Set data source for BindingSource1.
    BindingSource1.DataSource = customerTable;

    // Filter the items to show contacts who are owners.
    BindingSource1.Filter = "ContactTitle='Owner'";

    // Sort the items on the company name in descending order.
    BindingSource1.Sort = "Country DESC, Address ASC";

    // Set the data source for dataGridView1 to BindingSource1.
    dataGridView1.DataSource = BindingSource1;

}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft