question

Kennyqui-0889 avatar image
0 Votes"
Kennyqui-0889 asked JiachenLiMFST-9349 commented

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

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



dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Kennyqui-0889 avatar image
0 Votes"
Kennyqui-0889 answered JiachenLiMFST-9349 commented

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





image.png (663.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Kennyqui-0889 ,
Does every 'ORDER' have connection with 'SUGER LEVEL'?
I make some test base on the code you gave, and after adding a judgment to avoid ListBox2.Items(i) out of range, I get the following result:
147184-2021118.png


Here is my code.

         For i As Integer = 0 To ListBox1.Items.Count - 1
             If i < ListBox2.Items.Count Then
                 DataGridView1.Rows.Add(New Object() {ListBox1.Items(i).ToString, ListBox2.Items(i).ToString})
             Else
                 DataGridView1.Rows.Add(New Object() {ListBox1.Items(i).ToString, ""})
             End If
         Next

If I have any misunderstanding, please let me know.



1 Vote 1 ·
2021118.png (14.1 KiB)
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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



figure1.png (8.8 KiB)
figure2.png (21.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.