Grasshopper
Grasshopper

Reputation: 4977

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.

Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?

Windows Form

Upvotes: 17

Views: 45301

Answers (5)

ssvelu83
ssvelu83

Reputation: 11

Easy way is using the ColumnWidthChanging event

private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
      if (e.ColumnIndex == 0)
      {
           e.Cancel = true;
           e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
      }
}

Upvotes: 1

DanO
DanO

Reputation: 417

I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.

When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

Upvotes: 1

Libor
Libor

Reputation: 3303

You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.

You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.

Upvotes: 1

Rajesh
Rajesh

Reputation: 7876

Checkout the ListView HeaderStyle property. It has the following options:

  1. None
  2. Nonclickable
  3. Clickable

From MSDN:

The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns

Upvotes: 9

Scott Smith
Scott Smith

Reputation: 1863

You can set the HeaderStyle member of the ListView to None.

listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;

Upvotes: 63

Related Questions