How to add all item in listbox 1 to datagridview in column 1.Then for listbox2 to column 2 in datagridview

Kennyqui 216 Reputation points
2021-11-05T09:24:32.437+00:00

I have 2 listbox and 1 datagridview. Then 1 button for transferring the item.
My listbox1 is the order of customer.
My listbox2 is sugar level the item here is 3 only.

The problem when i run the code below some item in listbox1 did not tranfer in column 1 its 3 items transfer.

I want to transfer all item in listbox1 to datagridview in column 1 that the column 2 for listbox2 is 3 items only.

Pls help me i'm beginner so i'm sorry about this.

Here the code

For i As Integer = 0 To ListBox1.Items.Count - 1

  DataGridView1.Rows.Add(New Object() {ListBox1.Items(i).ToString, ListBox2.Items(i).ToString})

Next

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kennyqui 216 Reputation points
    2021-11-05T15:19:07.217+00:00

    I don't really understand but I will try to do it, to improve my knowledge and experience, thank you so much I appreciate a lot :)
    By the way below is my format , and this is simple ordering system form3 for the cashier that i been doing.
    if you have other idea feel free to comment in how to add multiple items in first column but the second colum is 3 item only in row

    146870-image.png


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2021-11-05T11:22:05.147+00:00

    Best way to work with a DataGridView is by using its DataSource with a DataTable in this case rather than no DataSource which means later you can access data via the underlying DataTable rather than via the Cells.

    Create columns in the designer, set each column's DataPropertyName to a DataColumn in the DataTable.

    146852-figure1.png

    Namespace CodeSamples  
     Partial Public Class Form1  
     Inherits Form  
      
     Public Sub New()  
     InitializeComponent()  
     End Sub  
      
     Private Sub Populate()  
     Dim table As New DataTable()  
     table.Columns.Add("FirstName", GetType(String))  
     table.Columns.Add("LastName", GetType(String))  
     dataGridView1.DataSource = table  
      
     FirstNameListBox.DataSource = New List(Of String)() From {"Karen", "Mary", "Jim"}  
     LastNameListBox.DataSource = New List(Of String)() From {"Payne", "Jones", "Smith"}  
      
     For index As Integer = 0 To FirstNameListBox.Items.Count - 1  
     table.Rows.Add( {FirstNameListBox.Items(index), LastNameListBox.Items(index) })  
     Next index  
     End Sub  
      
     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs)  
     Populate()  
     End Sub  
     End Class  
    End Namespace  
      
    

    146827-figure2.png

    Next level, use a BindingSource, here I show how to get the current row in the DataGridView.

    Namespace CodeSamples  
    	Partial Public Class Form1  
    		Inherits Form  
      
    		Private bindingSource As New BindingSource()  
    		Public Sub New()  
    			InitializeComponent()  
    		End Sub  
      
    		Private Sub Populate()  
    			Dim table As New DataTable()  
    			table.Columns.Add("FirstName", GetType(String))  
    			table.Columns.Add("LastName", GetType(String))  
    			bindingSource.DataSource = table  
    			dataGridView1.DataSource = bindingSource  
      
    			FirstNameListBox.DataSource = New List(Of String)() From {"Karen", "Mary", "Jim"}  
    			LastNameListBox.DataSource = New List(Of String)() From {"Payne", "Jones", "Smith"}  
      
    			For index As Integer = 0 To FirstNameListBox.Items.Count - 1  
    				table.Rows.Add( {FirstNameListBox.Items(index), LastNameListBox.Items(index) })  
    			Next index  
    		End Sub  
      
    		Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs)  
    			Populate()  
    		End Sub  
      
    		Private Sub GetCurrentButton_Click(ByVal sender As Object, ByVal e As EventArgs)  
    			If bindingSource.Current IsNot Nothing Then  
    				Dim row As DataRow = CType(bindingSource.Current, DataRowView).Row  
    				MessageBox.Show($"{row.Field(Of String)("FirstName")} {row.Field(Of String)("LastName")}")  
    			End If  
    		End Sub  
    	End Class  
    End Namespace  
      
    
    0 comments No comments