C# 如何隐藏 ListView ColumnHeader?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8989214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Hide ListView ColumnHeader?
提问by Grasshopper
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.
我正在努力找出用于包含表单中的预定义作业列表的正确控件。我目前在 Predefined Job Name 组中有一个 ListBoxControl,它列出了海事服务店的所有预定义工作(即换油、调整等...)。然后,根据在我的 ListBox 中选择的项目(即工作名称),我需要显示与该工作相对应的项目。例如,如果选择换油工作,我需要展示 4 夸脱油、1 个油过滤器、人工等……等等。
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?
目前,当我加载表单数据时,我有一个 DAO,它使用 LINQ to SQL 从数据库中检索我的所有作业。然后我遍历结果并将作业名称放入 ListBox。我遇到的问题是 ListBox 项目没有像 ListView 项目那样的标签。因此,每次用户在 ListBox 中选择另一个项目时,我都必须执行另一个 LINQ 查询以再次从数据库中获取作业,以便我可以显示其相应的项目。如果我可以使用 ListView 并隐藏列标题,我可以在标签上设置整个作业,这样每次用户选择一个新项目时,我都可以访问详细信息,而无需再次调用数据库。有没有办法可以隐藏 ListView 的列标题而不隐藏整个列?


采纳答案by Scott Smith
You can set the HeaderStylemember of the ListViewto None.
您可以将 的HeaderStyle成员设置ListView为None。
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
回答by Rajesh
Checkout the ListView HeaderStyleproperty. It has the following options:
签出 ListView HeaderStyle属性。它有以下选项:
- None
- Nonclickable
- Clickable
- 没有任何
- 不可点击
- 可点击
From MSDN:
来自 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
HeaderStyle 属性允许您指定列标题是否可见,或者如果它们可见,它们是否将用作可单击的按钮。如果HeaderStyle属性设置为ColumnHeaderStyle.None,则不会显示列标题,尽管ListView控件的项和子项仍按列排列
回答by Libor
You can also create simple object like ListItemwhich has two poperties: Text(string) and Tag(object). Then implement ListItem.ToString() and you can use these in the ListBoxas well.
您还可以创建简单的对象,例如ListItem具有两个属性:(Text字符串)和Tag(对象)。然后实现 ListItem.ToString() ,您也可以在ListBox.
You can also check out Better ListView Expresscomponent, which is free and allows displaying items in Detailsview without columns. The advantage over ListBox and ListView is a native look and many extra features.
您还可以查看Better ListView Express组件,该组件是免费的,允许在没有列的详细信息视图中显示项目。相对于 ListBox 和 ListView 的优势在于原生外观和许多额外功能。
回答by DanO
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.
我发现,如果你知道一个事实,你没有显示标题,最好将 HeaderStyle 属性设置为 None,正如 Rajesh 上面提到的。
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.
当屏幕最初加载时在 .CS 中设置时,显示标题直到屏幕完全呈现。
回答by ssvelu83
Easy way is using the ColumnWidthChangingevent
简单的方法是使用ColumnWidthChanging事件
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}

