Locked add listbox items to datagrid view

  • Thursday, May 24, 2012 1:16 PM
     
      Has Code

    I have 2 listboxes I want to insert items from listbox 1 in column 1 of the datagrid view

    and items from listbox 2 in column 2 of data grid view

    the below code will add items from listbox1 to column1 of the datagrid view :

    DataGridView1.ColumnCount = 2
            DataGridView1.Columns(0).Name = "File Name"
            DataGridView1.Columns(1).Name = "Status"
    
            Dim c As String = ListBox1.Items.Count
            Dim d As String = ListBox2.Items.Count
            
    
            Dim naffy(c) As String
            Dim naffy1(d) As String
            
    
    
    
    
            For Each xmlfilename As String In ListBox1.Items()
    
                naffy(c) = xmlfilename
    
                DataGridView1.Rows.Add(naffy(c))
    
            Next

    what can I do to insert the items of listbox 2 in column 2 of the data grid view

    any suggestion ???


    Georges Naffah

All Replies

  • Thursday, May 24, 2012 1:52 PM
     
     Answered Has Code

    Try the following if there are equal items in both ListBox controls

    For x As Integer = 0 To ListBox1.Items.Count - 1
        DataGridView1.Rows.Add(New Object() {ListBox1.Items(x).ToString, ListBox2.Items(x).ToString})
    Next


    KSG

    • Marked As Answer by Georges Naffah Thursday, May 24, 2012 2:12 PM
    •  
  • Thursday, May 24, 2012 2:13 PM
     
     

    worked exactly how i needed , can you please explain your code logic ?


    Georges Naffah

  • Thursday, May 24, 2012 3:12 PM
     
     

    worked exactly how i needed , can you please explain your code logic ?


    Georges Naffah

    Hello Georges,

    In short the for/next provides access to each item in both ListBox controls assuming they both have the same amount of items in the for/next we add a new row to the DataGridView Row collection. Best to review the properties and methods of each control to better understand this logic via the links provided which will assist in future coding when working with these controls. Hope this helps.


    KSG