Setting Listindex from Database table

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I filling a combo box with all 50 states and when I select a customer from a
list box I want to find the identity(listindex) in the combo box which is the
identity stored in the customer record. If I do not sort the states combo box
I'm able to find the correct state(listindex) everytime but if I sort the
combo box in ascending order it is not finding the correct state(index). I
used to do this all the time in VB6 and I'm not sure if it is possible in
VB.NET. Sample code below

Sub FillStateDropDown()
Dim i As Integer
Dim strName As String
Dim Iindex As Integer
objStateDA.FillSchema(objDataSet, SchemaType.Source, "States")
objStateDA.Fill(objDataSet, "States")

For i = 1 To objDataSet.Tables("States").Rows.Count
strName = objDataSet.Tables("States").Rows(i -
1).Item("StateAbrev")
Iindex = objDataSet.Tables("States").Rows(i - 1).Item("StateId")

cboState.DisplayMember = strName
cboState.ValueMember = Iindex
cboState.Items.Add(strName)
'cboState.Items.Insert(Iindex - 1, strName)
Next
End Sub

Private Sub lstNames_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lstNames.Click

Dim objRow As DataRow
objRow =
objDataSet.Tables("FamilyMembers").Rows.Find(lstNames.SelectedIndex + 1)

txtFirstNm.Text = objRow.Item("FirstNm")
txtMiddleNm.Text = objRow.Item("MiddleNm")
txtLastNm.Text = objRow.Item("LastNm")
txtSSN.Text = objRow.Item("ssn")
txtDOB.Text = objRow.Item("dob")
txtHomePhone.Text = objRow.Item("HomePhone")
txtWorkPhone.Text = objRow.Item("WorkPhone")
cboSex.SelectedItem = objRow.Item("sex")
txtStreet.Text = objRow.Item("street")
txtCity.Text = objRow.Item("city")
cboState.SelectedIndex = objRow.Item("StateId")
cboState.SelectedIndex = cboState.SelectedIndex - 1
txtNotes.Text = "" & objRow.Item("Notes")

End Sub
 
WEM,

Why not bind your cboState combo box directly to the States table? Then
you can set the DisplayMember to "StateAbrev" and ValueMember to
"StateId". This means that your ID is not tied to the ListIndex.

----
cboState.DataSource = objDataSet.Tables("States")
cboState.DisplayMember = "StateAbrev"
cboState.ValueMember = "StateId"
----

If you now set Sorted = True, your StateId's will still match to the
correct state. In order to access your state row, you'll need to do
something like this:

----
Dim dRow as DataRowView
Dim iStateId As Integer
Dim sStateAbbrev As String

dRow = DirectCast(cboState.SelectedItem, DataRowView)

iStateId = CInt(dRow("StateId"))
sStateAbbrev = CStr(dRow("StateAbrev"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VB.NET and Access 2002 1

Back
Top