Home > Software design >  How to add an indexed value into a list view coming from another different form?
How to add an indexed value into a list view coming from another different form?

Time:11-01

Hello I just recently learned C# and I am tasked to create a system, I want to know how to pass the values from Form1 and insert these values into a listview in Form2, tried searching but I couldn't understand the codes I found so please help me with a simple answer. Thank you very much.

I know how to pass value to another form but I want to learn specifically how to pass the value to another form then insert them on a listview with columns.

I want to know how to add items in a listview but from another form, I want to see exactly the sample coding for it.

CodePudding user response:

Try this:

private void AddItemsToListView()
{
   ListViewItem item1 = new ListViewItem("item1",0);
   item1.Checked = true;
   item1.SubItems.Add("1");
   item1.SubItems.Add("2");
   item1.SubItems.Add("3");
   ListViewItem item2 = new ListViewItem("item2",1);
   item2.SubItems.Add("4");
   item2.SubItems.Add("5");
   item2.SubItems.Add("6");
   ListViewItem item3 = new ListViewItem("item3",0);
   item3.Checked = true;
   item3.SubItems.Add("7");
   item3.SubItems.Add("8");
   item3.SubItems.Add("9");
   Form2.listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
   Form2.listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
   Form2.listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
   Form2.listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
   Form2.listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});
}

Then you can call this procedure from form1, and it will at some items to form2's listview. I assume that the listview control is named: listView1.

  • Related