Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Fill a Combo box from a dataset

$
0
0
Hello again.

Still pretty new to vb 2010, but starting to understand it as I use it.

I have a problem with filling my combobox, which I fill from a dataset. I can get my list to show in the combobox, and I can get the associated Primary Key values, but, I am missing something and I can't find the answer in any forums;

My combo fills from a dataset, but, instead of just a list of names from the dataset showing, I want to display the text "Assigned To" in the combo after it loads from the dataset so that the user knows what the combo contains. How do I do that? I used to do that in VB6 by first of all loading the recordset, and then typing Combo.Text = "Something or the other" at the end of the combo fill loop:

.............
.....
With cboActionTo
.Clear
For intCount = 1 To IntRecCount
.AddItem rsGeneric("Name")
.ItemData(.NewIndex) = rsGeneric("LogID")
rsGeneric.MoveNext
Next intCount
.Text = "Name"
End With


Secondly, Although this works, and I can retrieve the Primary Key (LogID) in the _SelectedIndexChanged event, I always start by getting the ID of the First Name on the list. I know why, it is the first record in the DataRowView value that is being shown..and how I have done it is probably not the correct way. How should I retrieve the Primary Key value for the selected name...without setting my variable to the first name ID in the list? I really can't find much of help using a Google search.

Thanks for any help that you may offer.

My code is as follows:

'*Get the dataset:

In a class module:

Public Function Fill_Login_Combos() As DataSet

'*Fill the Login Names etc combos

'*Open the connection
cnNEQAS.Open() '*Already defined elsewhere

strSql = "SELECT LogID, Name " _
& "FROM Login " _
& "WHERE(Retired = 0) AND (NOT (LogID IN (4, 16, 18))) " _
& "ORDER BY Name ASC"

Try
da = New SqlDataAdapter(strSql, cnNEQAS)
ds = New DataSet()
strSql = ""

da.Fill(ds, "Login") '*Fill the data adapter from the Data Set and give it a name
da.Dispose() '*Set the data adapter to nothing

Return ds '*Set the function to the dataset

Catch ex As Exception
MsgBox("Error in clsUser, Fill_Login_Combos", vbOKOnly Or vbInformation, Progname)
Return ds
Finally
ds.Dispose()
ds = Nothing
cnNEQAS.Close()
End Try

End Function

Fill the combo on the Windows form:

Private Sub FillCombo()
'*Fill the Log-In Combo

Dim UserDetails As New clsUser
Dim Tasks As DataSet

Tasks = UserDetails.Fill_Login_Combos()

cboActionTo.DropDownStyle = ComboBoxStyle.DropDownList
cboActionTo.Text = "Assigned to" '*Does nothing
cboActionTo.DisplayMember = "Name" '*The Name field
cboActionTo.ValueMember = "LogID" '*The Primary Key ID field
cboActionTo.DataSource = Tasks.Tables("Login")
cboActionTo.SelectedIndex = 0

End Sub

The combo_SelectedIndexChanged event:

Private Sub cboActionTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboActionTo.SelectedIndexChanged

ActionedTo = 0

'*This works, but when the combo loads, ActionedTo is set to the LogID of the first name in the list, I want it to be set to 0 initially.
'*Get the LogID of the assigned person
Dim row As DataRowView = CType(cboActionTo.SelectedItem, DataRowView)
If Not IsNothing(row) Then
ActionedTo = (row("LogID"))
End If


End Sub


Thanks for your time
Regards

DJ

Viewing all articles
Browse latest Browse all 27554

Trending Articles