0

Im new in using listview on vb.net, but i managed to show the data from mysql database, but somehow i got not an error but somethings strange happening on my list view.

this is the 1st image of the listview, enter image description here

but after i click the exit button and open it again. this happens enter image description here

the columns in listview doubles, every time i exit and re-open it again, but if i close the whole application and re-run it again, it back to normal and double again every time I click the exit and open the inventory again.

this is the code on form load.

Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    Call LVWloader()
    Call Locker(True)
    Call objLocker(False)


    With ListView1.Columns
        .Add("Semen ID", 50, HorizontalAlignment.Center)
        .Add("Bullname", 150, HorizontalAlignment.Center)
        .Add("Breed", 150, HorizontalAlignment.Center)
        .Add("Quantity", 50, HorizontalAlignment.Center)
        .Add("Location", 50, HorizontalAlignment.Center)
        .Add("Date Purchased", 50, HorizontalAlignment.Center)

    End With
    For i As Integer = 0 To ListView1.Columns.Count - 1
        ListView1.Columns(i).Width = -2
    Next i
End Sub

and this is the loader code

Public Sub LVWloader()
    Dim myCommand As New MySqlCommand
    Dim myReader As MySqlDataReader
    Dim conn As MySqlConnection
    conn = New MySqlConnection
    conn.ConnectionString = "server = localhost;username= root;password= a;database= semenis"
    Try
        conn.Open()
    Catch mali As MySqlException
        MsgBox("connot establish connection")
    End Try

    ListView1.Items.Clear()
    With myCommand
        .Connection = conn
        .CommandText = "Select * from inventory"
    End With
    myReader = myCommand.ExecuteReader
    While myReader.Read()
        With ListView1
            .Items.Add(myReader(0))
            With .Items(.Items.Count - 1).SubItems
                .Add(myReader.Item(1))
                .Add(myReader.Item(2))
                .Add(myReader.Item(3))
                .Add(myReader.Item(4))
                .Add(myReader.Item(5))

            End With
        End With
    End While
End Sub

i think the code error comes from .Add for listview, maybe theres an alternative way in doing this?

im using vb.net and mysql as database

thanks,

11
  • Why use a ListView at all? Why not populate a DataTable and simply bind that to a DataGridView? Commented Nov 21, 2016 at 2:54
  • i use datagridview on other tables, but on this one I guess I want to explore more on how to properly use listview, Commented Nov 21, 2016 at 2:57
  • As for why that's happening, I'm guessing that it's because you're displaying the same instance of that form multiple times and the Load event handler is executed each time. If you're going to construct the ListView on Load then make sure to dispose the instance after closing it and then create a new instance each time you want to display it. It would probably help us if you were to post the code that opens this form. Commented Nov 21, 2016 at 2:57
  • The way to properly use the ListView is not to use it when a DataGridView is the appropriate control. A ListView is NOT a grid control. If you're just displaying tabular data then use a DataGridView. Don't use a ListView unless you're using at least one View other than Details and/or you're using groups. Commented Nov 21, 2016 at 2:59
  • the 2nd code posted: Public sub LVWloader opens the form and its called in main_load, so i guess the issue is at the lvwloader, Commented Nov 21, 2016 at 3:14

1 Answer 1

2

The reason why it's duplicating is because you are using the form's Load event to add the columns, which is called each time the form is loaded. You need to either add the columns in the form designer or in the form's constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Or ensure that the Load event handler isn't executed multiple times. That said, I can't see any reason not to have added the columns in the designer.
thank you sir, I did two alternative answer, 1. is by 'using statement' from @jmcilhinney and 2. is I added the columns in the form designer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.