Jacob Latham Jacob Latham - 6 months ago 35
Vb.net Question

Select only certain values from binding source to be displayed in combobox

I am attempting to manipulate a combobox to only show certain values that I desire out of a binding source. Here is the code I currently have.

provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
datafile = "C:\Users\Jacob\Desktop\Halton\HaltonProject.accdb"
connString = provider & datafile
myConnection.ConnectionString = connString
myConnection.Open()

Dim str As String
str = "SELECT * FROM [Employee] WHERE (SP_Level <> 0)"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader


Dim Full_Name As String

While dr.Read
Full_Name = dr("Full_Name").ToString
SP_Emp1.Items.Add(Full_Name)
SP_Emp2.Items.Add(Full_Name)
SP_Emp3.Items.Add(Full_Name)
SP_Emp4.Items.Add(Full_Name)
End while


As it stands this code looks at the "SP_Level" and filters out any employees who have a "0" listed there. The string "Full_Name" returns all the employees who are listed as other that "0" in the "SP_Level" column.

I would like to leave the combobox databound to the EmployeeBindingSource while only displaying the new filtered "Full_Name" strings produced by the code above. I have tried filtering the bindingsource but that only returns the first alphabetical string to match then stops. I'm sure there is a simple solution I just can't seem to find it

Data bound combobox

Access table used in data-binding

I am quite new to VB programming so I apologize if this is poorly worded. Please let me know if I can better explain anything.

Answer

You can have/ use the same DataSource for each row/skill set, you just need to filter each accordingly. This creates a DataView for each criteria rather than manually filtering out some from the source or using a new query to create only slightly different sources:

' my fake table
Dim colors As String() = {"Red", "Green", "Blue", "Orange", "White"}
Dim dt As New DataTable
dt.Columns.Add("color", GetType(String))

For Each s As String In colors
    dt.Rows.Add(s)
Next

' create a view for each level and bind it
Dim SPView As New DataView(dt, "Color <> 'Red'", "", DataViewRowState.CurrentRows)
ComboBox1.DataSource = SPView
ComboBox1.DisplayMember = "color"

Dim XPView As New DataView(dt, "Color <> 'Green'", "", DataViewRowState.CurrentRows)
ComboBox2.DataSource = XPView
ComboBox2.DisplayMember = "color"

Dim ZPView As New DataView(dt, "Color <> 'Blue'", "", DataViewRowState.CurrentRows)
ComboBox3.DataSource = ZPView
ComboBox3.DisplayMember = "color"
Comments