1 Attachment(s)
Find Combo Item By ID and Reset SelectedIndex
Hello Folks i want to get ID of the combobox item and then reset the selected index of that combobox to that item. I populate my Combobox with a list of 56 Districts from an array so, the ID is hidden.
When i call the sub below which calls FindComboItemByID it gives me the error below, so the sub FindComboItemByID has some problem that i seem not to figure out...is there a better way of doing this?
VB Code:
Sub getDistValueByID()
Dim strSQL As String = "SELECT DistID FROM tblDistricts WHERE(District='" & Me.cbDistrict.Text & "')"
Dim strID As String = connMger.LookUp(strSQL)
FindComboItemByID(Me.cbDistrict, strID)
End Sub
VB Code:
Public Sub FindComboItemByID(ByVal cbName As ComboBox, ByVal strID As String)
' This sub is used to find an Item in a combobox/listbox and
' set the selected index of the combo box/listbox to that item
'On Error Resume Next
Dim bFound As Boolean
Dim itemCount As Integer
Dim lstItem As New ListItem
itemCount = 0
While Not bFound Or itemCount <= cbName.Items.Count - 1
lstItem = CType(cbName.Items(itemCount), ListItem)
If lstItem.ID = CInt(strID) Then
cbName.SelectedIndex = itemCount
bFound = True
End If
itemCount += 1
End While
If Not bFound Then
cbName.SelectedIndex = -1
End If
End Sub
Re: Find Combo Item By ID and Reset SelectedIndex
Use data-binding:
VB Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim arr(5) As District
For i As Integer = 0 To arr.GetUpperBound(0) Step 1
arr(i).ID = i
arr(i).Name = "District " & i
Next
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"
Me.ComboBox1.DataSource = arr
'Select the District object with an ID of 3.
Me.ComboBox1.SelectedValue = 3
End Sub
End Class
Public Structure District
Private _id As Integer
Private _name As String
Public Property ID() As Integer
Get
Return Me._id
End Get
Set(ByVal value As Integer)
Me._id = value
End Set
End Property
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
End Structure
Re: Find Combo Item By ID and Reset SelectedIndex
Thanks JMC...the problem is that the number of Districts is not static, it increases and decreases with time.At one time it can be 60 and at another time it can be 56 Districts.
And i want to find the combo item using the District text that is displayed in the combo.(I know this sounds contradictory to the thread title)
This is how i populate my District Combobox, DistID is hidden and District is shown.
VB Code:
Sub PopulateDistrictCombo()
Dim strSQL As String = "SELECT DistID,District FROM tblDistricts ORDER BY District ASC"
Me.cbDistrict.Items.Clear()
PopulateColumnCombo(Me.cbDistrict, strSQL, "District", "DistID", "tblDistricts")
End Sub
I see this is what your code does.
but after populating the combobox, i populate the combobox with a District within
a record i want to update. So, i would like to use the "District Text" to basically bring up the District with the same name
from the district items contained in the combobox.This will inturn bring up the hidden ID with the district and i would use it
to populate another combobox in the SelectedIndexChanged of the district combobox
Hope you get what i mean.
Re: Find Combo Item By ID and Reset SelectedIndex