Aan
Aan

Reputation: 12890

How to hide a column in a ListView control?

How can I hide a column in a ListView control, without setting the column Width property to 0?

Also, can I lock the Width of a column?

Upvotes: 13

Views: 64709

Answers (9)

AaA
AaA

Reputation: 3694

I was looking for a way to do the same thing which brought me here.

I'm not sure if there is a better way of doing it, so I use following workaround.

If you need to add a value to a Listview Item but don't want to show it, you can use a subitem index greater than the total column count. This way, even though the value exists it is not visible to the user.

Upvotes: 6

Masta
Masta

Reputation: 51

You should use DataTemplateSelector to hide and show dynamically a column or a row in a listview. There is an intersting article in the official documentation. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector

Upvotes: 1

Adam Bruss
Adam Bruss

Reputation: 1674

If you have only one column, a second empty column shows up to fill the extra space to the right. To hide this empty column, implement the listview resize event. In there do the following.

m_lstItems.Columns[0].Width = m_lstItems.Width - 5;

This will effecitvely hide the extra empty column and instead have the first column take up all the space in the listview.

Upvotes: 1

fabtrg
fabtrg

Reputation: 21

Go to Edit Columns (under properties of listview in design mode), under Misc set Width to ZERO

Upvotes: 1

Hamza Nashwan
Hamza Nashwan

Reputation: 171

The simplest way as follows, try this code segment :

hide:

      LVW.Columns.Item(0).Width = 0

show again:

      LVW.Columns.Item(0).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)

may this help someone.

Upvotes: 17

Nick
Nick

Reputation: 3337

If you need associate data with a row without displaying it use the ListView's Tag property

Upvotes: 8

anixrud
anixrud

Reputation: 194

Simply just remove column at the index you wish:

listView1.Columns.RemoveAt(3);

when you want it back just insert it with its name:

listView1.Columns.Insert(3, "Column Name");

It will back with its values.

Upvotes: 6

Silentium
Silentium

Reputation: 41

How to hide/show listview columns

C#, .NET framework 3.5.

It is easy to hide and show listview columns, if you use the listview in “virtual mode”. In “virtual mode”, you are responsible for filling the listviewitems with data. This makes it possible to put the correct data in the correct column.

Let me demonstrate: Create a form, and add a listview control and a button control. Add 3 columns to the listview control. Set the “view” property of the listview control to “Details”. Set the “VirtualMode” property of the listview control to “True”. Set the “VirtualListSize” property of the listview control to “100”. Add a bool to the form:

private bool mblnShow = true;

Add the event “RetrieveVirtualItem” for the listview control, and add the following code:

ListViewItem objListViewItem = new ListViewItem();
objListViewItem.Text = "Item index: " + e.ItemIndex.ToString();
if (mblnShow) objListViewItem.SubItems.Add("second column: " +     DateTime.Now.Millisecond.ToString());
objListViewItem.SubItems.Add("third column: " + DateTime.Now.Millisecond.ToString());
e.Item = objListViewItem;

Add the “Click” event for the button control, and add the following code:

mblnShow = !mblnShow;
if (mblnShow && !this.listView1.Columns.Contains(this.columnHeader2))   this.listView1.Columns.Insert(1, this.columnHeader2);
else if (!mblnShow && this.listView1.Columns.Contains(this.columnHeader2))
    this.listView1.Columns.Remove(this.columnHeader2);

Run the application, and press the button to show and hide the second column.

Please note that running a listview in virtual mode will throw an error if you put data in the items collection. There is much more the know about virtual mode, so I suggest reading about it before using it.

Upvotes: 2

Tigran
Tigran

Reputation: 62246

Not very clear what matters here C++Cli tag, but:

I presume that if you're talking about a columns in ListView, you're in details view of the control. There is no builtin Hide/Show column way in ListView, as much as I'm aware of, so one of solutions can be just remove that column form UI.

To do that in most smooth way possible just call your ListView column regeneration code in between

listView.SuspendLayout(true);

/*Manipulate column here*/

listView.ResumeLayout();

The data which is visible on ListView always remains "in your hands", so at the moment you will decide to show the column again, just show the column and fill ListView again.

Hope this helps.

Upvotes: 4

Related Questions