C# 在 listView 中创建列并添加项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11311153/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:21:14  来源:igfitidea点击:

Creating columns in listView and add items

c#visual-studio-2010listview

提问by 3D-kreativ

I'm learning how to use the listViewin a windowsFormand I have some problems that I hope to solve here. The first thing is when I'm creating the columnswith the code below:

我正在学习如何使用listViewin awindowsForm并且我有一些我希望在这里解决的问题。第一件事是当使用以下代码创建列时:

private void initListView()
    {
        // Add columns
        lvRegAnimals.Columns.Add("Id", -3,HorizontalAlignment.Left);
        lvRegAnimals.Columns.Add("Name", -3, HorizontalAlignment.Left);
        lvRegAnimals.Columns.Add("Age", -3, HorizontalAlignment.Left);
    }

When I run the program, the name of the columns are not visible, they are all at the left corner, and I have to "drag" them to be able to read the text. What have I done wrong?

当我运行程序时,列的名称是不可见的,它们都在左角,我必须“拖动”它们才能读取文本。我做错了什么?

And finally I wonder how I add items to the columns. Do I first create a object like

最后,我想知道如何将项目添加到列中。我是否首先创建一个对象

ListViewItem item1 = new ListViewItem(???);
item1.SubItems.Add("text");

Is each listViewItem objects a column or a row? How do I add rows of info? Preciate some help! Thanks!

每个 listViewItem 对象是一列还是一行?如何添加信息行?珍视一些帮助!谢谢!

采纳答案by Tom

Your first problem is that you are passing -3 to the 2nd parameter of Columns.Add. It needs to be -2 for it to auto-size the column. Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.columns.aspx(look at the comments on the code example at the bottom)

您的第一个问题是您将 -3 传递给 Columns.Add 的第二个参数。它需要为 -2 才能自动调整列大小。来源:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listview.columns.aspx(查看底部代码示例的注释)

private void initListView()
{
    // Add columns
    lvRegAnimals.Columns.Add("Id", -2,HorizontalAlignment.Left);
    lvRegAnimals.Columns.Add("Name", -2, HorizontalAlignment.Left);
    lvRegAnimals.Columns.Add("Age", -2, HorizontalAlignment.Left);
}

You can also use the other overload, Add(string). E.g:

您还可以使用其他重载 Add(string)。例如:

lvRegAnimals.Columns.Add("Id");
lvRegAnimals.Columns.Add("Name");
lvRegAnimals.Columns.Add("Age");

Reference for more overloads: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.columnheadercollection.aspx

更多重载参考:http: //msdn.microsoft.com/en-us/library/system.windows.forms.listview.columnheadercollection.aspx

Second, to add items to the ListView, you need to create instances of ListViewItem and add them to the listView's Items collection. You will need to use the string[] constructor.

其次,要将项目添加到 ListView,您需要创建 ListViewItem 的实例并将它们添加到 listView 的 Items 集合中。您将需要使用 string[] 构造函数。

var item1 = new ListViewItem(new[] {"id123", "Tom", "24"});
var item2 = new ListViewItem(new[] {person.Id, person.Name, person.Age});
lvRegAnimals.Items.Add(item1);
lvRegAnimals.Items.Add(item2);

You can also store objects in the item's Tag property.

您还可以将对象存储在项目的 Tag 属性中。

item2.Tag = person;

And then you can extract it

然后你可以提取它

var person = item2.Tag as Person;

Let me know if you have any questions and I hope this helps!

如果您有任何问题,请告诉我,我希望这会有所帮助!

回答by IamBatman

I didn't see anyone answer this correctly. So I'm posting it here. In order to get columns to show up you need to specify the following line.

我没有看到有人正确回答这个问题。所以我把它贴在这里。为了显示列,您需要指定以下行。

lvRegAnimals.View = View.Details;

And then add your columns after that.

然后在此之后添加您的列。

lvRegAnimals.Columns.Add("Id", -2, HorizontalAlignment.Left);
lvRegAnimals.Columns.Add("Name", -2, HorizontalAlignment.Left);
lvRegAnimals.Columns.Add("Age", -2, HorizontalAlignment.Left);

Hope this helps anyone else looking for this answer in the future.

希望这可以帮助其他人在未来寻找这个答案。

回答by Navin Pandit

You need to set property for the control:

您需要为控件设置属性:

listView1.View = View.Details;

回答by U Zay Yar

            listView1.View = View.Details;
        listView1.Columns.Add("Target No.", 83, HorizontalAlignment.Center);
        listView1.Columns.Add("   Range   ", 100, HorizontalAlignment.Center);
        listView1.Columns.Add(" Azimuth ", 100, HorizontalAlignment.Center);     

i also had same problem .. i drag column to left .. but now ok .. so let's say i have 283*196 size of listview ..... We declared in the column width -2 for auto width .. For fitting in the listview ,we can divide listview width into 3 parts (83,100,100) ...

我也有同样的问题..我把列拖到左边..但现在好了..所以假设我有283*196大小的listview...我们在列宽-2中声明了自动宽度..适合在列表视图中,我们可以将列表视图宽度分为 3 部分 (83,100,100) ...