Home > Software design >  The row is overwriting and not adding rows listview
The row is overwriting and not adding rows listview

Time:11-23

Im sending the data from form1(Home) to form2 (StatusReport) but the information taken from form 1 is not adding to the next row in the form2, instead it is overwriting the same row. I did it on the form 1, the adding per row but from sending the data from form 1 to form 2, it's not adding properly.

Form 1 code

Dim recipientName As String = TextBox5.Text
Dim address As String = TextBox6.Text
Dim contactNumber As String = TextBox7.Text
Dim deliveryMode As String = ComboBox3.SelectedItem
Dim deliveryDate As Date = DateTimePicker1.Value.Date

Form 2 code

Dim recipientName As String = Home.TextBox5.Text
Dim address As String = Home.TextBox6.Text
Dim contactNumber As String = Home.TextBox7.Text
Dim deliveryMode As String = Home.ComboBox3.SelectedItem
Dim deliveryDate As Date = Home.DateTimePicker1.Value.Date
Dim orderStatus As String = "Pending"

Dim str(6) As String
Dim lvItem As ListViewItem
str(0) = recipientName
str(1) = address
str(2) = contactNumber
str(3) = deliveryMode
str(4) = deliveryDate
str(5) = orderStatus
lvItem = New ListViewItem(str)
ListView1.Items.Add(lvItem)

CodePudding user response:

Creating a new ListViewItem using a String Array creates a single ListViewItem with the array signifying the sub items.

Maybe simpler to create the ListViewItems individually and add them, like this:

lv.Items.Add(New ListViewItem(recipientName))
lv.Items.Add(New ListViewItem(Address))
lv.Items.Add(New ListViewItem(contactNumber))
lv.Items.Add(New ListViewItem(deliveryMode))
lv.Items.Add(New ListViewItem(recipientName))

etc.

You may want to clear the items first:

lv.Items.Clear()

CodePudding user response:

It is unclear how you are going back and forth between the 2 forms but this should get you started. Set up the columns in the ListView before adding any items. You can either do this once in code or at design time. ListViews hold ListViewItems. The first column is the Text property and the following columns are SubItems.

Private Sub CreateLVColumns()
    'you will probably want to do this at design time
    With ListView1.Columns
        .Add("Name")
        .Add("Address")
        .Add("Number")
        .Add("Mode")
        .Add("Date")
        .Add("Status")
    End With
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lvi As New ListViewItem(Home.TextBox5.Text)
    With lvi.SubItems
        .Add(Home.TextBox6.Text)
        .Add(Home.TextBox7.Text)
        .Add(Home.ComboBox3.Text)
        .Add(Home.DateTimePicker1.Value.Date.ToString) 'you can add a format to the ToString
        .Add("Pending")
    End With
    ListView1.Items.Add(lvi)
End Sub
  • Related