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

Sort プロパティと Filter プロパティを使用すれば、BindingSource コントロールを並べ替え、フィルター処理できます。 基礎となるデータ ソースが IBindingList のとき、単純な並べ替えを適用できます。また、データ ソースが IBindingListView のときは、フィルター処理と高度な並べ替えを適用できます。 Sort プロパティには、標準の ADO.NET 構文が必要です。データ ソースに含まれるデータの列名を表す文字列の後に、一覧を昇順で並べ替えるか、降順で並べ替えるか示す ASC または DESC が続きます。 コンマ区切り記号で各コンマを区切ることで、高度な並べ替えや複数列の並べ替えを設定できます。 Filter プロパティは文字列式を受け取ります。

注意

接続文字列内に機密情報 (パスワードなど) を格納すると、アプリケーションのセキュリティに影響を及ぼすことがあります。 データベースへのアクセスを制御する方法としては、Windows 認証 (統合セキュリティとも呼ばれます) を使用する方が安全です。 詳細については、「接続情報の保護」を参照してください。

BindingSource でデータをフィルター処理するには

  • 必要な式に Filter プロパティを設定します。

    次のコード例では、式は、列名の後ろに、列に使用する値を付けたものになります。

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

BindingSource でデータを並べ替えるには

  1. 列名に Sort プロパティを設定し、後ろに ASCDESC を付けて昇順か降順か示します。

  2. コンマで複数の列を区切ります。

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

次のコードの場合では、Northwind サンプル データベースの Customers テーブルから DataGridView コントロールにデータを読み込み、表示されるデータをフィルター処理し、並べ替えます。

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 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;
}
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 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

コードのコンパイル

この例を実行するには、BindingSource1 と言う名前の BindingSource と、dataGridView1 という名前の DataGridView が含まれるフォームにコードを貼り付けます。 そのフォームの Load イベントを処理し、読み込みイベント ハンドラー メソッドで InitializeSortedFilteredBindingSource を呼び出します。

関連項目