简体   繁体   English

将项添加到Listview控件

[英]Add item to Listview control

I have a listview in c# with three columns and the view is details. 我在c#中有一个listview ,有三列,视图是详细信息。 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. 在此先感谢您的帮助。

// 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);

这是结果

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);
    }
}

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

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 源代码

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);
  • 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"); 

ListView控件使用Items集合将项目添加到控件中的listview ,并且能够自定义项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM