방법: Windows Forms 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를 사용하여 데이터를 정렬하려면
오름차순 또는 내림차순을 나타내도록 열 이름 뒤에 원하는
ASC
또는DESC
가 오도록 Sort 속성을 설정합니다.여러 열을 쉼표로 구분합니다.
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
를 호출합니다.
참고 항목
.NET Desktop feedback
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기