Dear Experts

I have following codes to to show data in listview.
It displays first column empty.
The value of first column is displayed in column2.
How to show value of first database field in column1

Please help

Dim StudentID As Integer
Dim Name As String
Dim Address As String
Dim Course As String

Dim row As DataRow

Dim item As ListViewItem
Dim str As String
Dim dt As DataTable

ListView1.View = View.Details
ListView1.Columns.Add("S #", 60, HorizontalAlignment.Center)
ListView1.Columns.Add("Name", 120, HorizontalAlignment.Left)
ListView1.Columns.Add("City", 120, HorizontalAlignment.Left)
ListView1.Columns.Add("Contact", 80, HorizontalAlignment.Left)

Str = "select * from employees"
dt = GetTable(Str)

For Each row In dt.Rows()

StudentID = row("sno")
Name = row("Name")
Address = row("city")
Course = row("phone")

item = New ListViewItem

item.SubItems.Add(StudentID)
item.SubItems.Add(Name)
item.SubItems.Add(Address)
item.SubItems.Add(Course)

ListView1.Items.Add(item)

Next
Me.ListView1.Focus()
Me.ListView1.Items(0).Selected = True

' this function is in main module
Public Function GetTable(ByVal mstr As String)
cmd = New SqlClient.SqlCommand(mstr, con)
da = New SqlClient.SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
Return (dt)
End Function

You can do:

item = New ListViewItem(StudentID)
item.SubItems.Add(Name)
item.SubItems.Add(Address)
item.SubItems.Add(Course)

or

item = New ListViewItem
item.text=StudentID
item.SubItems.Add(Name)
item.SubItems.Add(Address)
item.SubItems.Add(Course)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.