ADO.NET データを Windows フォーム BindingSource コンポーネントで並べ替えおよびフィルター処理する方法
Sort プロパティと Filter プロパティを使用して、BindingSource コントロールの並べ替えとフィルター処理の機能を公開できます。 基になるデータ ソースが IBindingListの場合は単純な並べ替えを適用できます。また、データ ソースが IBindingListViewの場合は、フィルター処理と高度な並べ替えを適用できます。 Sort プロパティには、標準の ADO.NET 構文が必要です。データ ソース内のデータの列の名前を表す文字列の後に ASC
または DESC
して、リストを昇順または降順で並べ替える必要があるかどうかを示します。 各列をコンマ区切り記号で区切ることで、高度な並べ替えまたは複数列の並べ替えを設定できます。 Filter プロパティは文字列式を受け取ります。
手記
接続文字列内にパスワードなどの機密情報を格納すると、アプリケーションのセキュリティに影響する可能性があります。 Windows 認証 (統合セキュリティとも呼ばれます) を使用すると、データベースへのアクセスをより安全に制御できます。 詳細については、「接続情報の保護」を参照してください。
BindingSource を使用してデータをフィルター処理するには
Filter プロパティを目的の式に設定します。
次のコード例では、式は列名の後に列に必要な値が続きます。
BindingSource1.Filter = "ContactTitle='Owner'";
BindingSource1.Filter = "ContactTitle='Owner'"
BindingSource を使用してデータを並べ替えるには
Sort プロパティを列名に設定し、その後に
ASC
またはDESC
して昇順または降順を示します。複数の列をコンマで区切ります。
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
を呼び出します。
関連項目
- Sort
- Filter
- 方法: サンプル データベース をインストールする
- BindingSource コンポーネント
.NET Desktop feedback