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

x
?
Solved

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

Posted on 2014-01-02
4
Medium Priority
?
299 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
[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
  • 2
4 Comments
 
LVL 13

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 2000 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

Technology Partners: 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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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