4
Answers

Hiding a column in a ListView

Ask a question
buff daemon

buff daemon

16y
22.3k
1

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

 


Answers (4)