Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
Hiding a column in a ListView
Posted on: 12 Aug 2008

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

 


AuthorReply
Re: Hiding a column in a ListView
Posted on: 12 Aug 2008  

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:

<ItemTemplate>
<tr>
<td>Column1</td>
<td>Column2</td>
<%if (<PROTECTED_VARIABLE> == "TEXT_TO_EVELUTE_CONDITION_TRUE") { %>
<td>Column3</td>
<%} %>
<td>Column4</td>
</tr>
</ItemTemplate>

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/


Regards,
Niradhip(James Hetfield)
MCP,MVC,MVP(Mindcracker)
--------------------------------------------------------------------------------------
Please remember to click “Do you like this answer” on the post that helps you.
Re: Hiding a column in a ListView
Posted on: 12 Aug 2008  
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.
Re: Hiding a column in a ListView
Posted on: 12 Aug 2008  

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


Regards,
Niradhip(James Hetfield)
MCP,MVC,MVP(Mindcracker)
--------------------------------------------------------------------------------------
Please remember to click “Do you like this answer” on the post that helps you.
Re: Hiding a column in a ListView
Posted on: 23 Jun 2009  
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
Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.