Solved

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

Posted on 2014-01-02
4
293 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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