Solved

Help with selecting multiplr rows from listbox control using VB.NET

Posted on 2014-01-02
4
290 Views
Last Modified: 2014-02-01
Hi,

I loaded my listbox control from an xml file, when i use the code below to select mutiple rows, I receive the following error:

Operator '+' is not defined for string "," and type 'DataRowView'.


On line:

tempstring += "," + item

The error does not occur if I add the countries in the items collection of the listbox control, but I need to load the control from the xml file.


How di i fix this problem?


 Private Sub ListBox1_Click(sender As Object, e As System.EventArgs) Handles ListBox1.Click
        tempstring = ""

        For Each item As Object In Me.ListBox1.SelectedItems

            tempstring += "," + item

        Next

        If tempstring <> "" Then



            tempstring = Microsoft.VisualBasic.Right(tempstring, Len(tempstring) - 1)


        End If

        Me.C1TrueDBGrid45.Columns("Receiver").Text = tempstring
            End Sub


Code to load listbox from Form load event.

  fsReceiver1 = New System.IO.FileStream(Application.StartupPath + "\Receiver.xml", IO.FileMode.Open)
        dtsetReceiver.Clear()
        dtsetReceiver.ReadXml(fsReceiver1)
        fsReceiver1.Close()
        Me.ListBox1.DataSource = dtsetReceiver.Tables(0)
        Me.ListBox1.DisplayMember = "Receiver"

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
4 Comments
 
LVL 12

Expert Comment

by:ktaczala
ID: 39753211
Try tempstring = tempstring & "," & item, instead of  tempstring += "," + item
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39753434
I'm guessing you've bound your listbox to something that is returning a DataTable. When you are trying to append, you are trying to append a DataRowView object, rather than the text within the field being displayed.

You probably need something more like:
tempstring &= "," & item.Item("NameOfColumn").ToString()

Open in new window

0
 

Author Comment

by:vcharles
ID: 39755305
Hi,

Unfortunately both approaches did not work, when I tried the first solution I received the following error:
Operator '&' is not defined for string "," and type 'DataRowView'.

When I tried the last approach(tempstring &= "," & item.Item("Receiver").ToString()
) in the code below, for some reason, all the rows in the Grid dissapear when I select a row from the listbox. Any ideas why the rows in the Grid dissapears?

Private Sub ListBox1_Click(sender As Object, e As System.EventArgs) Handles ListBox1.Click
        tempstring = ""

        For Each item As Object In Me.ListBox1.SelectedItems
                      tempstring &= "," & item.Item("Receiver").ToString()
              Next
        If tempstring <> "" Then
            tempstring = Microsoft.VisualBasic.Right(tempstring, Len(tempstring) - 1)
        End If
        Me.C1TrueDBGrid45.Columns("Receiver").Text = tempstring
            End Sub

Thanks.

V.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39755850
Probably because this line:
Me.C1TrueDBGrid45.Columns("Receiver").Text = tempstring

Open in new window

Is setting the value for the entire column, not just for the current row.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now