Using a LINQ query to populate a combo box with data from an access database - vb.net

In VB.Net:
I am trying to populate a combo box on my form using data from a MS Access database. Specifically; taking all of the last names from the database, sorting them in ascending order in an output list which I named players and then adding each item in players to my combo box (cboPlayer).
Public Sub GetPlayers()
Dim PlayerLastName As New List(Of String)
PlayerLastName.Add("Smith")
PlayerLastName.Add("Hill")
PlayerLastName.Add("Beyer")
PlayerLastName.Add("Wilson")
PlayerLastName.Add("Hudson")
PlayerLastName.Add("van Zegeren")
PlayerLastName.Add("Bibbs")
PlayerLastName.Add("Muller")
PlayerLastName.Add("Pierce")
PlayerLastName.Add("Henry")
PlayerLastName.Add("Johnston")
Dim Players = From Last In PlayerLastName
Order By Last Ascending
Select Last
cboPlayer.Items.Add(Players.ToString)
cboPlayer.SelectedItem = 0
I know this isn't exactly correct but not positive what direction to head in. When I run the program the combo box is populated with System.linq.enumerated.
Any ideas what that might mean or what I am doing incorrectly?

Maybe over thinking it just a little? Why not try without the linq, List has a sort function. Also just bind PlayerLastName to the combobox.
Public Sub GetPlayers()
Dim PlayerLastName As New List(Of String)
PlayerLastName.Add("Smith")
PlayerLastName.Add("Hill")
PlayerLastName.Add("Beyer")
PlayerLastName.Add("Wilson")
PlayerLastName.Add("Hudson")
PlayerLastName.Add("van Zegeren")
PlayerLastName.Add("Bibbs")
PlayerLastName.Add("Muller")
PlayerLastName.Add("Pierce")
PlayerLastName.Add("Henry")
PlayerLastName.Add("Johnston")
PlayerLastName.Sort()
cboPlayers.DataSource = PlayerLastName
cboPlayers.SelectedIndex = -1
End sub
Edit:
or if you still want to us linq then, you need to change the linq return of IEnumerable to a List...
Dim Players = From Last In PlayerLastName
Order By Last Ascending
Select Last
ComboBox1.DataSource = Players.ToList
ComboBox1.SelectedIndex = -1

Related

Filter DataGridView across all columns

Sorry for my bad English. I am an old German man, I want to filter a DGV across all columns. In the Moment I use following filter:
Dim Such_Spalte = Me.DtS_DGV.DTT_1.article_1Column.ColumnName
But I have 10 Columns (article_1 to Article_10)
If as an example Sugar is in column 1,3,5 I want to see all records where sugar occurs.
I hope this is understandable
You seem to be using strongly typed datasets and as such I would expect that your datagridview is bound through a BindingSource (the windows forms designer sets it up this way, and my presumption is that you used te designer to do your binding)
If your datagridview is indeed bound through a bindingsource:
Dim bs = DirectCast(datagridviewX.DataSource, BindingSource)
Dim sb = New StringBuilder() 'it will hold the filter string
For Each col in DtS_DGV.DTT_1.Columns
sb.AppendFormat("[{0}] = '{1}' OR ", col.ColumnName, "Sugar")
Next col
sb.Length -= 3 'remove the trailing OR
bs.Filter = sb.ToString()
If your datagridview is bound direvtly to the table, it will have attached to the table's DefaultView property, a DataView, which also has a Filter that works in the same way. If this is the case, do the same thing with the loop to build the filter string, and then:
DtS_DGV.DTT_1.DefaultView.Filter = sb.ToString()

Check if combobox contains value of list vb.net

I have list with duplicates values
Dim lstPlayerValidate As New List(Of Integer)
Dim duplicates = lstPlayerValidate.Where(Function(x) lstPlayerValidate.Where(Function(y) x = y).Count() > 1).Distinct().ToList
Now I want check if comboxes contains any value from this list.
I do it so:
If cmbPlayer.Items.Contains(duplicates) Then 'do something
My combobox has Item as List(Of Player), where PlayerID is value which I want compare. Perhaps I'm doing an unnecessary job creating another list with only id values. But I don't now how I can do it

How to iterate display values in a combobox

I have a combobox whose values (displayvalue) are formatted (from a database query):
John Doe (11111)
where 11111 is the userID. The UserID is the login name for their machine and I want to default the selected value to the login UserID.
Since combobox.findstring(UserID) only matches if the entry begins with that string, I need to iterate through the values to look for the substring of UserID in the entries.
I've searched around here, but solutions seems to land all around this specific example. I can't seem to find a method that returns the display value at a specific index. What am I missing?
EDIT:
This is how I am populating my combobox:
Private Sub PopulateDropdown(strSQl As String, objControl As ComboBox, strTextField As String, strDataField As String)
Dim objdatareader As New DataTable
objdatareader = DataAccess.GetDataTable(strSQl)
objControl.DataSource = objdatareader
objControl.DisplayMember = strTextField
objControl.ValueMember = strDataField
End Sub
This may actually help folks trying to find the ValueMember of a combobox as well.
I am populating my combobox from a datatable, so this solution may only be valid from that. I am not really sure why this works. I just stumbled upon it.
First, I started by doing a for each item in combobox.items. According to intelisense, there is no property of .value, .text, .DisplayMember, or anything related to that. I did notice that the return type on combobox.items is a DataRowView. I am not sure why that is, but I went with it. One of the members of DataRowView is Row. It turns out, each column from the DataTable is added to the Row collection in 'item's' DataRowView. Rows(0) is the first column, Row(1) is the second, etc. I was then able to look in the Row's full text to find my userid, and then select that row by using the FindExactString of the combobox. The below code works (I built the datatable manually in this example):
dim UserID As String="12345"
dim MyTable as New Datatable
MyTable.Columns.Add("Value", Type.GetType("System.String"))
MyTable.Columns.Add("Text", Type.GetType("System.String"))
MyTable.rows.add("1","Bob Smith(11223)"
MyTable.rows.add("2","George Brown(12345)"
cboAssignedID.datasource=MyTable
cboAssignedID.DisplayMember="Text"
cboAssignedID.ValueMember="Value"
For Each item In cboAssignedID.Items
If InStr(item.Row(1).ToString, UserID) > 0 Then
cboAssignedID.SelectedIndex = cboAssignedID.FindStringExact(item.Row(1).ToString)
End If
Next

How to generate a random selection from SQL DB to a listbox using vb.net

I am trying to write a program that will allow a user to generate a random list of names. I have a gridview of names from a SQL Db when the form launches. Is it possible to generate a random list from the names in the gridview or does that have to come from another Sql Connection string and reference different parameters? I was trying to display random names from the gridview to a listbox. Thank you.
Here is the code that I have been trying to experiment with:
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
Dim listCount As Integer
Dim i As Integer = 0
Dim rnd As New Random
Dim listselection As Integer
listCount = grdEmployees.
Do While i < CInt(grdEmployees.Text)
'randomize selection
listselection = rnd.Next(0, grdEmployees.Items.Count)
lstSelected.Items.Add(grdEmployees.Items(listselection))
grdEmployees.Items.RemoveAt(listselection)
'increment i
i += 1
Loop
txtQuantity.Text = String.Empty 'Clears box after entry
End Sub
You could do it through your SQL query:
SELECT TOP 25 SomeField FROM SomeTable ORDER BY RAND()
Or through your managed code. Which is best depends on the size of the table and where you want the sorting to be done. If you prefer to sort on the server, or locally.

Use formula for multiplication?

I have two fields on a record ("Qty" and "Harga").
How do I multiply the two and save result into another field in a ListView?
The ListView control has no built-in ability to perform calculations for you, like a spreadsheet. It just displays whatever data you give it to display. If you want it to display the product of a multiplication equation, you will need to do that calculation yourself in the code and then add the result to the ListView column. For instance:
Public Sub AddItem(description As String, total As Integer, count As Integer)
Dim i As ListViewItem = ListView1.Items.Add(description)
i.SubItems.Add(total.ToString())
i.SubItems.Add(count.ToString())
Dim product As Integer = total * count
i.SubItems.Add(product.ToString())
End Sub