Community discussion forum

hiding the first column in a ListView

  • 1 year ago
    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.

    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
  • 1 year ago

     

    Hello,


    We do not have the option to hide or set visibility of a ListViewColumn to False.

    However, we can either remove the column or probably try setting the width to 0.

    Me.ListView1.Columns(1).Width = 0

    I hope this will help.

    Regards,

    Allen

     

  • 1 year ago

    Thanks Allen, it works.

  • 1 year ago

    anyway I am using ListViewItem.Tag for storing relevant data for item. It is as more efficient to read (It takes relatively huge time when storing data in visually processed properties and parts of controls), and, also it allows to store for example whole Customer (e.g.) instance with all additional data in Tag, and for last it is more conventionally right to do it like that. Anyway for testing is col.Width=0 good enough.Wink

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!