Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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

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
Avatar of ktaczala
ktaczala
Flag of United States of America image

Try tempstring = tempstring & "," & item, instead of  tempstring += "," + item
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

Avatar of Victor  Charles

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial