Add item to Listview control

后端 未结 6 1572
-上瘾入骨i
-上瘾入骨i 2020-11-28 06:31

I have a listview in 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 t

相关标签:
6条回答
  • 2020-11-28 07:06

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:11
    • 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");
      
    0 讨论(0)
  • 2020-11-28 07:17

    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

    0 讨论(0)
  • 2020-11-28 07:20

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

    0 讨论(0)
  • 2020-11-28 07:23

    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);
     ....
    
    0 讨论(0)
  • 2020-11-28 07:24

    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);
    
    0 讨论(0)
提交回复
热议问题