Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET Winforms Multiselect Listbox Traverse ?

Posted on 2009-03-31
8
Medium Priority
?
943 Views
Last Modified: 2012-05-06
In vb.net 2005, winforms, how do you traverse a multiselect Listbox ? I need to extract the selected values, and build a SQL Select statement.  I can write the loop, but Listbox.Selected() is not recognized (must be for Webforms). Thank you.
0
Comment
Question by:cgaDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:cdaly33
ID: 24035708
0
 

Author Comment

by:cgaDesign
ID: 24035734
No ... that example refers to Classic ASP ... I am using VB.NET WinForms.
0
 
LVL 9

Expert Comment

by:cdaly33
ID: 24035802

    Dim strWhere As String = "WHERE sClass IN("
 
 
    Dim i As Integer = 0
    Dim str As String = ""
    For i = 0 To ListBox1.SelectedItems.Count - 1
      strWhere &= "'" & ListBox1.SelectedItems(i).ToString() & "',"
    Next
 
 
    strWhere = strWhere.TrimEnd(",") & ")"

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:cgaDesign
ID: 24035848
The value of my strWhereUnit variable becomes, during output:
     Or P.intUnitFK = 'System.Data.DataRowView'
for each Selected Item. I am supposed to get integers as values.

Dim strWhere As String = ""
 For i = 0 To Me.lstLkUnit.SelectedItems.Count - 1
    strWhereUnit += " Or P.intUnitFK = '" + Me.lstLkUnit.SelectedItems(i).ToString() + "'"
 Next i

Thank you
0
 
LVL 9

Accepted Solution

by:
cdaly33 earned 600 total points
ID: 24035901
Oh you've databound it.  Try this where DataColumnName is the name of the column that contains that value that you want.
Dim strWhere As String = ""
 For i = 0 To Me.lstLkUnit.SelectedItems.Count - 1
    strWhereUnit += " Or P.intUnitFK = '" + CType(Me.lstLkUnit.SelectedItems(i), DataRowView).Item("DataColumnName").ToString() + "'"
 Next i

Open in new window

0
 

Author Comment

by:cgaDesign
ID: 24035970
OK, that worked.  Guess I need to look up CType, and bound Listboxes. My Listbox is actually bound to a bindingsource object.  This probably should have been worth more points ...
Thanks much !

0
 

Author Closing Comment

by:cgaDesign
ID: 31565150
The essence was that I needed to realize that I was traversing a DATABOUND Listbox.
0
 

Author Comment

by:cgaDesign
ID: 24035998
The essence to the problem was that I needed to realize that I was trying to traverse a DATABOUND Winforms Listbox.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question