How do I add to a specific column in Listview item

2020-07-14 09:09发布

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:

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.

6条回答
混吃等死
2楼-- · 2020-07-14 09:46

ListViewItems aren't aware of your ListView columns.

In order to directly reference the column, you first need to add all the columns to the 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:

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...

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.

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!

查看更多
相关推荐>>
3楼-- · 2020-07-14 09:58

I'm usially inherit ListViewItem as :

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"}));
查看更多
我命由我不由天
4楼-- · 2020-07-14 09:58
 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
查看更多
▲ chillily
5楼-- · 2020-07-14 10:02

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.

查看更多
贼婆χ
6楼-- · 2020-07-14 10:07

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)

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

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

Used DataReader to make 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 it is then I add the column using 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

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.

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

查看更多
叛逆
7楼-- · 2020-07-14 10:09
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.

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

查看更多
登录 后发表回答