Programmatically Select item in ComboBox where ValueMember is known
-
Hello, I've got a form that I create to show certain information about a specfied ClientID. I also load a combo box of all the Clients, so that the user can also pick up (and use) information that is currently associated with other clients. When I load the combo box of all Clients, I also want to set the Combo box to show the specified client, for continuity. I can't seem to work out how to do this! A brief overview of how I load the combo box is as follows:
Dim strProc As String = "usp_ClientSubDetails_LoadControls" Dim dsClients As New DataSet Dim drClients As DataRow Dim iRowCtr As Integer = 0 Dim iSelectionVal As Integer = 0 Dim iLoop As Integer ' to find the client in the combo box 'return dataset with client list in it If Not fncConnectSelectDataset(strProc, CommandType.StoredProcedure, dsClients, sysSystemData.SchedulerDb) Then strError = strProc Return False End If 'make sure there's records in the table If dsClients.Tables(0).Rows.Count = 0 Then strError = "No Rows Returned" Return False End If For Each drClients In dsClients.Tables(0).Rows 'DataLoader is a class that has an ID property and a DisplayValue property Me.cboClients.Items.Add(New DataLoader(drClients.Item(0), drClients.Item(1))) Next Me.cboClients.DisplayMember = "DisplayValue" Me.cboClients.ValueMember = "ID"
This gives me a combo box loaded with a bunch of DataLoader objects. By doing
CType(cboClients.SelectedItem,DataLoader).ID
I can retrieve the ClientID... I just don't know how t
-
Hello, I've got a form that I create to show certain information about a specfied ClientID. I also load a combo box of all the Clients, so that the user can also pick up (and use) information that is currently associated with other clients. When I load the combo box of all Clients, I also want to set the Combo box to show the specified client, for continuity. I can't seem to work out how to do this! A brief overview of how I load the combo box is as follows:
Dim strProc As String = "usp_ClientSubDetails_LoadControls" Dim dsClients As New DataSet Dim drClients As DataRow Dim iRowCtr As Integer = 0 Dim iSelectionVal As Integer = 0 Dim iLoop As Integer ' to find the client in the combo box 'return dataset with client list in it If Not fncConnectSelectDataset(strProc, CommandType.StoredProcedure, dsClients, sysSystemData.SchedulerDb) Then strError = strProc Return False End If 'make sure there's records in the table If dsClients.Tables(0).Rows.Count = 0 Then strError = "No Rows Returned" Return False End If For Each drClients In dsClients.Tables(0).Rows 'DataLoader is a class that has an ID property and a DisplayValue property Me.cboClients.Items.Add(New DataLoader(drClients.Item(0), drClients.Item(1))) Next Me.cboClients.DisplayMember = "DisplayValue" Me.cboClients.ValueMember = "ID"
This gives me a combo box loaded with a bunch of DataLoader objects. By doing
CType(cboClients.SelectedItem,DataLoader).ID
I can retrieve the ClientID... I just don't know how t
try cboClients.Items.FindByText("TheText").Selected = True Or cboClients.Items.FindByValue("TheValue").Selected = True oops! I think you asked for windows forms. I have answered for asp.net. Sorry. Last modified: 14-June-2006 6:39:52 AM --
-
try cboClients.Items.FindByText("TheText").Selected = True Or cboClients.Items.FindByValue("TheValue").Selected = True oops! I think you asked for windows forms. I have answered for asp.net. Sorry. Last modified: 14-June-2006 6:39:52 AM --
-
You're correct, it is Windows Forms. I can do cboClients.FindValue, but I don't have the string!
Can't you use
Overloads Public Function FindStringExact(String) As Integer
-
Can't you use
Overloads Public Function FindStringExact(String) As Integer
-
I may well be being incredibly dense, but I don't know the string; I only know the ID of the Client. So I wouldn't have a String to pass the function, right? Thanks for your help so far! Tom
Maybe you could loop through each of the elements in the combobox and set the Item with the client id selected. Something like this...
Pseudocode
for each item in combobox.items if item.value = clientid then item.selected = true end if
-
Maybe you could loop through each of the elements in the combobox and set the Item with the client id selected. Something like this...
Pseudocode
for each item in combobox.items if item.value = clientid then item.selected = true end if
-
Thanks Dinuj... Your pseudocode got me there in the end....
Dim dl As DataLoader For Each dl In cboClients.Items If dl.ID = ClientID Then cboClients.SelectedItem = dl End If Next
Nice one! Give this man a cigar!! Regards, Tom
mincefish wrote:
Nice one! Give this man a cigar!!
:) Glad to be of your help.