C# 如何添加到 Listview 项目中的特定列?

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

How do I add to a specific column in Listview item?

c#.netwinformslistviewuser-interface

提问by Dark Star1

I create a listview and the number of columns are determined at runtime. I have been reading texts on the web all over about listview( and I still am) but I wish to know how to add items to a specific column in listview I thought something like:

我创建了一个列表视图,列数是在运行时确定的。我一直在网上阅读有关 listview 的文本(现在仍然如此),但我想知道如何将项目添加到 listview 中的特定列,我想的是:

m_listview.Items.Add("1850").SubItems.Add("yes");

would work assuming the "1850"which is the text of the column would be the target column.

假设作为列文本的“1850”将成为目标列。

采纳答案by Sk93

ListViewItems aren't aware of your ListView columns.

ListViewItems 不知道您的 ListView 列。

In order to directly reference the column, you first need to add all the columns to the ListViewItem.

为了直接引用列,首先需要将所有列添加到 ListViewItem 中。

So... lets say your ListView has three columns A, B and C; This next bit of code will only add data to column A:

所以...假设您的 ListView 有三列 A、B 和 C;下一段代码只会将数据添加到 A 列:

ListViewItem item = new ListViewItem();
item.text = "Column A";

m_listView.Items.Add(item);

To get it to add text to column C as well, we first need to tell it it has a column B...

为了让它也向 C 列添加文本,我们首先需要告诉它它有一个 B 列......

ListViewItem item = new ListViewItem();
item.text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");

m_listView.Items.Add(item);

So now we'll have columns A, B and C in the ListViewItem, and text appearing in the A and C columns, but not the B.

所以现在我们将在 ListViewItem 中有 A、B 和 C 列,并且文本出现在 A 和 C 列中,但不是 B。

Finally... now that we HAVE told it it has three columns, we can do whatever we like to those specific columns...

最后......既然我们已经告诉它它有三列,我们可以对那些特定的列做任何我们喜欢的事情......

ListViewItem item = new ListViewItem();
item.text = "Column A";
item.SubItems.Add("");
item.SubItems.Add("Column C");

m_listView.Items.Add(item);

m_listView.Items[0].SubItems[2].Text  = "change text of column C";
m_listView.Items[0].SubItems[1].Text  = "change text of column B";
m_listView.Items[0].SubItems[0].Text  = "change text of column A";

hope that helps!

希望有帮助!

回答by Jesper Palm

Something like this maybe?

也许像这样的东西?

ListViewItem item = m_listview.Items.Add("1850");
item.Subitems.Add(string.Empty); //Make an add method that does this for every column you got and then you can use the indexer below.
item.Subitems[1].Text = "Test";

Index 0 is the main item. Index 1 > are the subitems.

索引 0 是主要项目。索引 1 > 是子项。

回答by Chernikov

I'm usially inherit ListViewItem as :

我通常将 ListViewItem 继承为:

public class MyOwnListViewItem : ListViewItem 
{
    private UserData userData;

    public MyOwnListViewItem(UserData userData) 
    {
        this.userData = userData;
        Update();
    }

    public void Update() 
    { 
       this.SubItems.Clear(); 
       this.Text = userData.Name; //for first detailed column

       this.SubItems.Add(new ListViewSubItem(this, userData.Surname)); //for second can be more
    }
}

where

在哪里

public class UserData 
{
   public string Name;
   public string Surname;
}

using

使用

listView1.Items.Add(new MyOwnListViewItem(new UserData(){Name="Name", Surname = "Surname"}));

回答by Kenny Mann

ListViewItem item = new ListViewItem("foo");
item.SubItems.Add("foo2");
this.listView1.Items.Add(item);

ListViews don't support databinding and strongly typed naming. I would recommend considering using a DataGridView instead. Depending on what you need, it may save yourself some sanity points.

ListViews 不支持数据绑定和强类型命名。我建议考虑使用 DataGridView 代替。根据您的需要,它可能会为您节省一些理智点。

Chernikov has it nicely implemented to make things a bit saner.

Chernikov 很好地实现了它,使事情变得更加理智。

回答by EdmondJ

This is what I do because the columns are added at run time via the users preference.

这就是我所做的,因为这些列是在运行时通过用户首选项添加的。

I am using a listView to display information from a database to the user. ( I probably should use DataGrid, but when I first made the program DataGrid didn't have built in full row select and I was too big of a newb to go through the custom control examples I found)

我正在使用 listView 向用户显示数据库中的信息。(我可能应该使用 DataGrid,但是当我第一次制作程序时 DataGrid 没有内置全行选择,而且我太大了,无法浏览我发现的自定义控件示例)

The user selects which columns they want to show up out of the total, how wide, and the title text.

用户从总数中选择他们想要显示的列、宽度和标题文本。

I created a user preferences table that saved information about the columns to add

我创建了一个用户首选项表,其中保存了有关要添加的列的信息

  • ColumnVisible = bool
  • ColumnText = string = text to display in header
  • columnWidth = int
  • ColumName = string = the exact name this column will refer to in my other database
  • ColumnIndex = int = the display index of the column
  • ColumnVisible = bool
  • ColumnText = string = 要在标题中显示的文本
  • 列宽 = 整数
  • ColumName = string = 此列将在我的其他数据库中引用的确切名称
  • ColumnIndex = int = 列的显示索引

I also made a class ColumnPreferencesand added a property for each of those columns

我还创建了一个类ColumnPreferences并为每一列添加了一个属性

Used DataReader to make List<ColumnPreferences>

使用 DataReader 制作 List<ColumnPreferences>

I sort this list according to display index, so I can iterate through it in the order the columns are displayed.

我根据显示索引对这个列表进行排序,所以我可以按照列的显示顺序遍历它。

I do an if statement checking if the next preference in preferences is visible.

我做了一个 if 语句,检查首选项中的下一个首选项是否可见。

If it is then I add the column using ListView1.Columns.Add(ColumnName, ColumnText, ColumnWidth);

如果是,那么我使用添加列 ListView1.Columns.Add(ColumnName, ColumnText, ColumnWidth);

At this point the columns are all set, to save where the user drags them around to I do basically the opposite. I iterate through all column in Listview.columns and save the column display index and column width back into my user preference table

在这一点上,列都设置好了,以保存用户将它们拖动到的位置,我基本上相反。我遍历 Listview.columns 中的所有列并将列显示索引和列宽保存回我的用户首选项表

When loading the actual data from the table I use this same list of column preferences to check whether I should display the data.

从表中加载实际数据时,我使用相同的列首选项列表来检查是否应该显示数据。

it is again sorted by display index, I check if it is visible, if it is not I skip it and go to the next one

它再次按显示索引排序,我检查它是否可见,如果不可见,我跳过它并转到下一个

if it is visible I use myDataReader[ ColumnPreference.ColumnName ]to look up the approprate ordinal for the data I want.

如果它是可见的,我会myDataReader[ ColumnPreference.ColumnName ]用来查找我想要的数据的适当序数。

I just add each of those results, in order, to a list view item / subitems then add them to the list view

我只是按顺序将这些结果中的每一个添加到列表视图项/子项中,然后将它们添加到列表视图中

回答by satya

 For i = 0 To listAcheivments.Items.Count - 1
    Dim arrparam2(,) As String
    arrparam2 = New String(,) {{"@emp_code", txtEmpCode.Text}, {"@ach_year", CDate("01-01-" & listAcheivments.Items(i).SubItems(0).Text)}, {"@acheivement", listAcheivments.Items(i).SubItems(1).Text}}

    SaveData(arrparam2, "insert_acheivements")

Next