次の方法で共有


ADO.NET データを Windows フォームの BindingSource コンポーネントを使用して並べ替えおよびフィルター処理する方法

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

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

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

コードのコンパイル

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

こちらも参照ください