slister
slister

Reputation: 789

VB.NET ListView is showing all but one of my columns from my dataset

I have a ListView control called lvSearchResults that I want to fill from my dataset. The dataset is filled from a SQL query, and for some reason it only displays 2 out of 3 columns and it shifts them over to the right one.

enter image description here

It puts the PID number in the last name column and the last name in the first name column. and it doesn't display the first name at all. The red bar is where I censored the numbers for privacy reasons, but it is displaying the PID number there.

Here is my code

Try

            mySelectAdapter.SelectCommand = New iDB2Command("SELECT EESSN,EENAML,EENAMF     FROM CARSPROD.EMPMSTR WHERE EENAML LIKE '" + txtLName.Text.ToUpper.Trim + "%' ORDER BY EENAML ASC", myConnection)

            mySelectAdapter.Fill(dsSearchResults)

            mySelectAdapter.SelectCommand.Dispose()


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

        lvSearchResults.Clear()

        lvSearchResults.Cursor = Cursors.WaitCursor

        lvSearchResults.View = View.Details
        lvSearchResults.GridLines = True
        lvSearchResults.FullRowSelect = True
        lvSearchResults.HideSelection = False
        lvSearchResults.MultiSelect = False

        lvSearchResults.Columns.Add("PID", 55, HorizontalAlignment.Right)
        lvSearchResults.Columns.Add("Last Name", 55, HorizontalAlignment.Left)
        lvSearchResults.Columns.Add("First Name", 55, HorizontalAlignment.Left)

        MsgBox(lvSearchResults.Columns.Count.ToString)

        If dsSearchResults.Tables(0).Rows.Count > 0 Then

            Dim lv As ListViewItem

            For i = 0 To dsSearchResults.Tables(0).Rows.Count - 1

                lv = New ListViewItem

                lv.SubItems.Add(dsSearchResults.Tables(0).Rows(i)(0))
                lv.SubItems.Add(dsSearchResults.Tables(0).Rows(i)(1))
                lv.SubItems.Add(dsSearchResults.Tables(0).Rows(i)(2))

                lvSearchResults.Items.Add(lv)

            Next

I am totally stumped on this one. If anybody has any ideas I would greatly appreciate it.

Upvotes: 0

Views: 772

Answers (1)

Remember that the ListViewItem.Text will show in Col(0), so when you add 3 subitems, you end up adding one more than needed and are not supplying any text for the first column (the LVI "label" itself). This should work:

For i = 0 To dsSearchResults.Tables(0).Rows.Count - 1

    lv = New ListViewItem
    lv.Text = dsSearchResults.Tables(0).Rows(i)(0)

    lv.SubItems.Add(dsSearchResults.Tables(0).Rows(i)(1))
    lv.SubItems.Add(dsSearchResults.Tables(0).Rows(i)(2))

    lvSearchResults.Items.Add(lv)

Next

You only need to add 2 subitems because the .Text of the LVI itself shows in the first column. A DataGridView would be even easier - just set the DataSource.

Upvotes: 1

Related Questions