Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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

"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
  • 2 weeks later...
Posted

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.

Experience is something you don't get until just after the moment you needed it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...