Share via


HOW TO:使用 Windows Form BindingSource 元件排序和篩選 ADO.NET 資料

更新:2007 年 11 月

您可以透過 SortFilter 屬性,公開 (Expose) BindingSource 控制項的排序和篩選功能。當基礎資料來源是 IBindingList 時可套用簡單排序,當基礎資料來源是 IBindingListView 時則可套用篩選和進階排序。Sort 屬性需要使用標準 ADO.NET 語法,也就是在代表資料來源中資料行名稱的字串之後跟隨著 ASC 或 DESC,以便指出清單應該依遞增或遞減順序來排序。可以利用逗號來分隔每個資料行,藉此設定進階排序或多重資料行排序。Filter 屬性可接受字串運算式。

注意事項:

在連接字串內儲存機密資訊 (例如密碼) 會影響應用程式的安全性。使用 Windows 驗證 (也稱為整合式安全性) 是控制資料庫存取的更安全方式。如需詳細資訊,請參閱保護連接資訊 (ADO.NET)

若要使用 BindingSource 來篩選資料

  • Filter 屬性設定為您要的運算式。

    在下列程式碼範例中,運算式為資料行名稱之後跟隨著您要的資料行值。

BindingSource1.Filter = "ContactTitle='Owner'"
         BindingSource1.Filter = "ContactTitle='Owner'";

若要使用 BindingSource 來排序資料

  1. Sort 屬性設定為您要的資料行名稱,之後跟隨著 ASC 或 DESC,以便指出是遞增順序或遞減順序。

  2. 以逗號分隔多重資料行。

BindingSource1.Sort = "Country DESC, Address ASC"
         BindingSource1.Sort = "Country DESC, Address ASC";

範例

下列程式碼範例會將 Northwind 範例資料庫的 Customers 資料表中的資料載入 DataGridView 控制項,然後再篩選並排序所顯示的資料。

Private Sub InitializeSortedFilteredBindingSource()

    ' Create the connection string, data adapter and data table.
    Dim connectionString As New SqlConnection("Initial Catalog=Northwind;" & _
        "Data Source=localhost;Integrated Security=SSPI;")
    Dim customersTableAdapter As New SqlDataAdapter("Select * from Customers", _
        connectionString)
    Dim customerTable As 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


End Sub

        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;

        }

編譯程式碼

若要執行這個範例,請將程式碼貼至表單,且該表單包含名為 BindingSource1 的 BindingSource 以及名為 dataGridView1 的 DataGridView。處理表單的 Load 事件,並且在載入事件處理常式方法中呼叫 InitializeSortedFilteredBindingSource。

請參閱

工作

HOW TO:安裝範例資料庫

參考

Sort

Filter

其他資源

BindingSource 元件