I want one listbox on a vb.net form to display records in a datatable that have "Selected = 0" (note: "Selected is a field in the datatable) and another listbox on the form to display records in the datatable that have "Selected = 1". I wrote the posted code, but when I set the filter for bs2 it appears to affect both listboxes, even though the first listbox's datasource is bs1. Why does this happen, and how can I set a different filter for each listbox?
Private Sub SelectItems_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim cn As New SqlConnection(strConn) Dim SQL As String SQL = "SELECT UserId As Id, FullName As Name, 0 As Selected FROM Users WHERE Terminated = 0" Using da As New SqlDataAdapter(SQL, cn) da.Fill(dt) End Using bs1.DataSource = dt bs1.Sort = "Name" bs1.Filter = "Selected = 0" lbxUnSelectedItems.DataSource = bs1 lbxUnSelectedItems.ValueMember = "Id" lbxUnSelectedItems.DisplayMember = "Name" bs2.DataSource = dt bs2.Sort = "Name" bs2.Filter = "Selected = 1" lbxSelectedItems.DataSource = bs2 lbxSelectedItems.ValueMember = "Id" lbxSelectedItems.DisplayMember = "Name" cn = Nothing End Sub
Exactly what Shaun stated. Here is a simple proof of concept:
Open in new windowForm1.Designer.vb -
Open in new windowProduces the following output -
Initial load:Setting the filter for the first listbox:Now setting the filter for the second listbox:If we click the 'Change Data Sources' button to use separate datasources: