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
Niradhip ChakrabortyRank #428 · 5.8K pointsPosted Aug 12, 2008, 1:03 PM EDT
Use inline coding to write condition into your .aspx page and u will be able to hide the column of Listview for particular criteria.
Try tis:
<%if ( == "TEXT_TO_EVELUTE_CONDITION_TRUE") { %>
<%} %>
above code will be hide the column3 & shows only 3 column (Column1, Column2, Column4)when if condition evolute true otherwise will be display all 4 columns.
You also can refer the following URL:
http://forums.msdn.microsoft.com/en-US/vblanguage/thread/b50a404a-ae4c-4cb2-92a0-db159aeba767/
buff daemon2 pointsPosted Aug 12, 2008, 1:11 PM EDT
Niradhip ChakrabortyRank #428 · 5.8K pointsPosted Aug 12, 2008, 2:02 PM EDT
You can hide a listview column by settings its Width property to 0 (zero.) Set myListView.Columns(0).Width = 0 and myListView.Columns(1).Width = -2 (auto-size) after you load your data.
You also can refer the following article.
http://www.codeproject.com/KB/list/listview.aspx
Karthikeyan2 pointsPosted Jun 23, 2009, 12:22 PM EDT