Share via


方法 : Windows フォーム BindingSource コンポーネントで ADO.NET データを並べ替える/フィルタ処理する

更新 : 2007 年 11 月

Sort プロパティと Filter プロパティを使用して、BindingSource コントロールの並べ替え機能とフィルタ機能を公開できます。基になるデータ ソースが IBindingList のときは単純な並べ替えを適用できます。また、データ ソースが IBindingListView のときはフィルタ処理と高度な並べ替えを適用できます。Sort プロパティは、標準の ADO.NET 構文を必要とします。この構文では、データ ソース内のデータ列の名前を表す文字列に続けて、リストの並べ替えを昇順、降順のどちらで行うかを示す ASC または DESC を指定します。高度な並べ替えや複数列の並べ替えを設定するには、コンマ (区切り記号) で各列を区切ります。Filter プロパティには文字列式を設定します。

ya3sah92.alert_note(ja-jp,VS.90).gifメモ :

接続文字列内にパスワードなどの機密情報を格納すると、アプリケーションのセキュリティに影響を及ぼすことがあります。データベースへのアクセスを制御する方法としては、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 サンプル データベースの顧客テーブルから 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 イベントを処理し、Load イベント ハンドラ メソッド内で InitializeSortedFilteredBindingSource を呼び出します。

参照

処理手順

方法 : サンプル データベースをインストールする

参照

Sort

Filter

その他の技術情報

BindingSource コンポーネント