The following code example loads data from the Customers table of the Northwind sample database into a DataGridView control, and filters and sorts the displayed data.
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;
}