determing selections in multiple selection listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rmarma
    New Member
    • Sep 2006
    • 3

    determing selections in multiple selection listbox

    sample code from intro to vb using .net (dana l.wyatt)
    is as follows
    *************** *************** **
    Private Sub btnShowAll_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnShowAll.Clic k
    If lstAnimals.Sele ctedItems.Count < 1 Then
    Exit Sub
    End If
    Dim s As String
    Dim index As Integer
    s = lstAnimals.Sele ctedItems.Item( 0)
    For index = 1 To lstAnimals.Sele ctedItems.Count - 1
    s &= ", " & lstAnimals.Sele ctedItems.Item( index)
    Next
    MessageBox.Show (s, "Selected Animals")
    End Sub
    *************** ************
    and it works
    but in my program it doesnt
    I have to add .ToString to avoid
    Cast from type 'DataRowView' to type 'String' is not valid.
    but then it prints "System.Data.Da taRowView" instead of the value member I need
    *************** *************** ********
    my code

    If ListBox1.Select edItems.Count < 1 Then
    Exit Sub
    End If
    Dim s As String
    Dim index As Integer
    s = ListBox1.Select edItems.Item(0) .ToString
    For index = 1 To ListBox1.Select edItems.Count - 1
    Console.WriteLi ne(ListBox1.Sel ectedItems.Item (index).ToStrin g)
    Next
    MessageBox.Show (s, "Selected Animals")


    any assistance would be greatly appreciated

    also get invalid cast when try
    Dim s As String = ""
    For index As Integer = 0 To lstAnimals.Sele ctedItems.Count - 1
    Dim drv As DataRowView = CType(lstAnimal s.SelectedItem( index), DataRowView)
    s &= drv.Item("name or index of field") & ControlChars.Cr Lf
    Next
    MessageBox.Show (s, "Selected Animals")
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    but then it prints "System.Data.Da taRowView" instead of the value member I need
    That is because you are not at the right object. From the DataRowView you need to get at your correct piece of data (i.e. which column do you want it from?)

    Try
    [code=vbnet]
    lstAnimals.Sele ctedItems.Item( index)("MyColum nName")
    [/code]

    Comment

    • rmarma
      New Member
      • Sep 2006
      • 3

      #3
      get "expression is not a method" when try it
      there is only one column in listbox and I want to get the corresponding value

      Comment

      • rmarma
        New Member
        • Sep 2006
        • 3

        #4
        Originally posted by rmarma
        get "expression is not a method" when try it
        there is only one column in listbox and I want to get the corresponding value
        this is what eventually worked

        Dim SelectItems As DataRowView
        For Each SelectItems In ListBox1.Select edItems
        Console.WriteLi ne("Display Member = {0}", SelectItems.Row .ItemArray(0))
        Console.WriteLi ne("Value Member = {0}", SelectItems.Row .ItemArray(1))
        Next

        thanks all

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Sorry, I was attempting to make VB up in my head, for me those were all [] brackets. Since VB likes to use () for everything I wasn't sure what to use.
          You should still be able to reference things by column name though

          Comment

          Working...