Hiding the first column in ListView

buffdaemon_live

New member
Joined
Jun 13, 2008
Messages
3
Programming Experience
Beginner
Is it possible to hide a listview control column? I want to have the first column hidden but with value held in it. I have Columns like StudentID, Student Name and StudentAddress and i am using StudentID as a primary key that is used to manipulate database. Below is my code. My code given below populates the list view. This function takes the SQL as query string and ListView as the name of the control.

VB.NET:
Sub populateListView(ByVal sql As String, ByVal mylView As ListView)
    'Clear Old items of ListView
    mylView.Items.Clear()
    mylView.Columns.Clear()
    Try
        Dim con As SqlConnection = dbConnect()
        Dim sqlCmd As New SqlCommand(sql)
        sqlCmd.Connection = con
        con.Open()
        sqlCmd.ExecuteNonQuery()
        Dim dr As SqlDataReader = sqlCmd.ExecuteReader
        Dim sCtr As Integer
        'Read the columns from data reader and add to ListView
        For sCtr = 0 To dr.FieldCount - 1
            Dim lvwColumn As New ColumnHeader
            lvwColumn.Text = dr.GetName(sCtr)
            mylView.Columns.Add(lvwColumn)
        Next
        Do While dr.Read
            Dim lvItem As New ListViewItem
            lvItem.Text = dr(0)
            For sCtr = 1 To dr.FieldCount - 1
                If dr.IsDBNull(sCtr) Then
                    lvItem.SubItems.Add("")
                Else
                    lvItem.SubItems.Add(dr.GetString(sCtr))
                End If
            Next sCtr
            mylView.Items.Add(lvItem)
        Loop
        dr.Close()
        sqlCmd.Dispose()
        con.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
 
Set the Width of the ColumnHeader to 0. To prevent user resizing it visible you can handle the ListView ColumnWidthChanging event.
 
Back
Top