Solved

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

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

Industry Leaders: 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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