Skip to content

Hiding a column in a ListView

buff daemon

Asked Aug 12, 2008, 12:49 PM EDT4 answers
Share
LinkedInX

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

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Niradhip Chakraborty
    0

    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:



    Column1
    Column2
    <%if ( == "TEXT_TO_EVELUTE_CONDITION_TRUE") { %>
    Column3
    <%} %>
    Column4

    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/

    0
  • 0
    I don't understand asp.net can you please give me any idea about Hiding a Column in ListView in vb.net. That would be very helpful.
    0
  • Niradhip Chakraborty
    0

    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

    0
  • 0
    You can set the column width to 0. You may want to refer my site for the sample code. http://assertonexception.blogspot.com/2009/06/how-to-showhide-columns-in-listview.html
    0