C# 将项目添加到 Listview 控件

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

Add item to Listview control

c#winformslistview

提问by amedeiros

I have a listviewin c# with three columns and the view is details. I need to add a item to each specific column but I am having a hard time with this. I have tried several things. Here is what I got so far. Thanks for any help in advance.

listview在 c# 中有一个三列,视图是详细信息。我需要向每个特定的列添加一个项目,但我很难做到这一点。我已经尝试了几件事。这是我到目前为止所得到的。提前感谢您的任何帮助。

// Add the pet to our listview
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add(pet.Name);
lvi.SubItems.Add(pet.Type);
lvi.SubItems.Add(pet.Age);

listView.Items.Add(lvi);

Here is the result

这是结果

采纳答案by Robbie

I have done it like this and it seems to work:

我已经这样做了,它似乎有效:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string[] row = { textBox1.Text, textBox2.Text, textBox3.Text };
        var listViewItem = new ListViewItem(row); 
        listView1.Items.Add(listViewItem);
    }
}

回答by Raj Ranjhan

The first column actually refers to Text Field:

第一列实际上是指文本字段:

  // Add the pet to our listview
    ListViewItem lvi = new ListViewItem();
    lvi.text = pet.Name;
    lvi.SubItems.Add(pet.Type);
    lvi.SubItems.Add(pet.Age);

    listView.Items.Add(lvi);

Or you can use the Constructor

或者你可以使用构造函数

 ListViewItem lvi = new ListViewItem(pet.Name);
 lvi.SubItems.Add(pet.Type);
 ....

回答by Pravin Dahale

Simple one, just do like this..

简单的一个,就这样做..

ListViewItem lvi = new ListViewItem(pet.Name);
    lvi.SubItems.Add(pet.Type);
    lvi.SubItems.Add(pet.Age);
    listView.Items.Add(lvi);

回答by pringston

Add items:

添加项目:

arr[0] = "product_1";
arr[1] = "100";
arr[2] = "10";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);

Retrieve items:

检索项目:

productName = listView1.SelectedItems[0].SubItems[0].Text;
price = listView1.SelectedItems[0].SubItems[1].Text;
quantity = listView1.SelectedItems[0].SubItems[2].Text;

source code

源代码

回答by Nigel

The ListView control uses the Items collection to add items to listviewin the control and is able to customize items.

ListView 控件使用 Items 集合向控件中的列表视图添加项目,并且能够自定义项目。

回答by Santosh Kokatnur

  • Very Simple

    private void button1_Click(object sender, EventArgs e)
    {
        ListViewItem item = new ListViewItem();
        item.SubItems.Add(textBox2.Text);
        item.SubItems.Add(textBox3.Text);
        item.SubItems.Add(textBox4.Text);
        listView1.Items.Add(item);
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
    }
    
  • You can also Do this stuff...

        ListViewItem item = new ListViewItem();
        item.SubItems.Add("Santosh");
        item.SubItems.Add("26");
        item.SubItems.Add("India");
    
  • 很简单

    private void button1_Click(object sender, EventArgs e)
    {
        ListViewItem item = new ListViewItem();
        item.SubItems.Add(textBox2.Text);
        item.SubItems.Add(textBox3.Text);
        item.SubItems.Add(textBox4.Text);
        listView1.Items.Add(item);
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
    }
    
  • 你也可以做这个东西......

        ListViewItem item = new ListViewItem();
        item.SubItems.Add("Santosh");
        item.SubItems.Add("26");
        item.SubItems.Add("India");