Solved

Get ValueMembers from a Databound Multiselect ListBox

Posted on 2004-08-12
6
4,426 Views
Last Modified: 2008-06-08
Hi everybody,
I am kinda stuck with this. Let me explain...

This is a Windows Application. I have a listbox that is bound to a dataset table.

        SqlCon.Open()
        DA.Fill(DS)         'DA is Adapter, DS is DataSet
        SqlCon.Close()
        ListBox1.DisplayMember = "EmpName"
        ListBox1.ValueMember = "EmpID"
        ListBox1.DataSource = DS.Tables(0)

Everything works fine. Now all I need is to get the ValueMembers for selected items in the list box. Lets make a string with all the values...

        Dim itm As Object
        Dim ValueString As String = "Selected Values are : "
        For Each itm In ListBox1.SelectedItems
            ValueString &= ListBox1.SelectedValue.ToString() & ", "
       Next
       MsgBox(ValueString)

===========================
What I am getting now:

If I select only a single Item, I get the right value.

But, when I select multiple items, it gives me the first value all the time. What I meant is, say I selected 3 items, and my first VALUE is 40. Then my ValueString is : "Selected Values are : 40, 40, 40, "... this is what I am getting.

Need to get it right ...  anybody ??

-Baan
0
Comment
Question by:DotNetLover_Baan
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 11786127
here ya go

        Dim ValueString As String = "Selected Values are : "

        Dim i As Integer
        For i = 0 To ListBox1.SelectedItems.Count - 1
            ValueString += ListBox1.SelectedItems.Item(i) + ", "
        Next

~b
0
 
LVL 18

Author Comment

by:DotNetLover_Baan
ID: 11786349
no good...
let me make it clearer...  I am trying to get the "SelectedValue"s behind the displayed Items.
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 500 total points
ID: 11787125
ok, got it.....this should be what you need

        Dim ValueString As String = "Selected Values are : "

        Dim i, index, count As Integer
        count = ListBox1.SelectedItems.Count - 1

        For i = 0 To count
            ValueString += ListBox1.SelectedValue.ToString + ", "

            index = ListBox1.SelectedIndices.Item(0)
            ListBox1.SetSelected(index, False)
        Next

        MsgBox(ValueString)

becuase it couldnt deal with the multiple selections, i deseleted each value as i added it to the string.

~b
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Author Comment

by:DotNetLover_Baan
ID: 11787857
Still having problem with... >>  ValueString += ListBox1.SelectedValue.ToString + ", " <<

Anyway I found a work arround. Instead of multi-selection, I am coding for the DoubleClick event of the ListBox. I am making the user to work with single item at a time.

I will leave this post open for couple of days, for a decent solution to this. I am also trying. If I don't find any solution , I will award you the points for working with me.
BUT , please don't disregard this question, its a very important matter, we never thought.
Thanks for your help... :)
-Baan
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 11788292
what problem are you having, i set up everything, dataset, adapter, valuemember, displaymember, everything....and it works perfectly for me....

'this gets the first value (just like you got)
ValueString += ListBox1.SelectedValue.ToString + ", "

'this gets teh index of the selected value
index = ListBox1.SelectedIndices.Item(0)

'this deselects the first value (so you have another "first" one the next loop around)
ListBox1.SetSelected(index, False)

if you go into the debugger, itll show you that the number of selected indices shrinks.  let me  know your problem, i spent a couple hours trying to figure this out....

0
 
LVL 18

Author Comment

by:DotNetLover_Baan
ID: 11789145
Thanks for your help.
I know this is working. I tested it, and it works fine for me too. But the application I am working on is quite a bit different. Everytime I tried that, it selects System.DataView... this happens when there is some error on selecting data or, when somehow table inside the dataset becomes NULL. I guess my problem is somewhere else. Anyway, thanks for you help. By the way... you did a real good job here. Thanks again.
You have answered my question perfectly... you deserve the points. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 46
VB.Net - MemoryMappedFiles - Confirm receipt 2 35
Help with error in Query 2 38
Need syntax help building hyperlink 2 39
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 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