CJLeit Posted January 15, 2007 Posted January 15, 2007 I have a combo box that I want to loop through all of the items in it and if the string returned from a function is in the list items I want to set the text to that string. I found a way to do it but it seems like there must be a better way to do it. Here is the way I'm currently doing it. Head is the string returned from my function. Any suggestions? For Each row As System.Data.DataRowView In cboHead.Items If (row.Row.Item(0).ToString) = head Then cboHeadead.Text = head Exit For End If Next Quote
Leaders snarfblam Posted January 15, 2007 Leaders Posted January 15, 2007 That looks about right to me. Quote [sIGPIC]e[/sIGPIC]
*Experts* Nerseus Posted January 16, 2007 *Experts* Posted January 16, 2007 If you just want a single string in the combo, and not a DataRowView, you could use the ComboBox's IndexOf method using your variable "head". If you have the original DataView you used to populate the drop-down then you might be able to use it's FindRows method to get a DataRowView array. Assuming your filter would get you exactly one row, you could use that in the ComboBox's IndexOf method. For example (my VB is rusty): Dim dv as DataView ' Assume set earlier '... ' Here is the filter, to find matching rows Dim matchingItems As DataRowView() = dv.FindRows(head) If matchingItems.Length > 1 Then cboHead.SelectedIndex = cboHead.IndexOf(matchingItems(0)) End If -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
travisowens Posted January 30, 2007 Posted January 30, 2007 Call me a nit picker but I would assign "head" to a variable outside the look and reference to it, preferably at the beginning of your method. I hate to absolutely hardcode something like that inside a loop, especially when the loop is much larger, you never know when you'll need to change it and imho defining the target string is best put at the top of the method. Quote Experience is something you don't get until just after the moment you needed it
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.