How to add items on listview (each column) - c#

I tried using this code to add items on ListView but clearly I only add one column on each rows although I have 10 columns. Here's my code:
ListView1.Items.Add(firstname.Text)
ListView1.Items.Add(middlename.Text)
ListView1.Items.Add(lastname.Text)
ListView1.Items.Add(gender.Text)
ListView1.Items.Add(age.Text)
ListView1.Items.Add(address.Text)
ListView1.Items.Add(lrnNumber.Text)
ListView1.Items.Add(formerschool.Text)
ListView1.Items.Add(strandcourse.Text)
ListView1.Items.Add(contact.Text)
ListView1.Items.Add(birthdate.Text)

You should create the object ListViewItem at first:
ListViewItem item = new ListViewItem(new []{"1","2","3","4"});
listView1.Items.Add(item);

Related

See no Items after adding them

My Listview show nothing after adding Items from a List into it. Why?
My List is not empty And my program goes into this loop.
So Is the code wrong for adding items into an listview? Because I saw many chatrooms and also from Microsoft that I can add them like this.
I tried it also with this code:listView1.Items.Add(new ListViewItem { ImageKey = "Person", Text = database1[counter1].name });
Here a picture of my imglist, and the list is chosen at thelistview:
enter image description here
You can't add items if there are no columns, or you can but they wont show up.
Have you added some columns to your listView?
//Adds needed columns
this.listView1.Columns.Add("<column_name>", 50);
If you have one column, you can simply add items by:
ListViewItem itm = new ListViewItem("my_item");
listView1.Items.Add(itm);
If you have multiple columns instead of a string, you can do the same but with a string array where the array size equals to the number of columns.
string[] items = new string[listView1.Columns.Count];
Try this in your code too, in my code only works with this:
this.listView1.View = View.Details;

How to get selected index when used to Ctrl+A key to select rows in grid

How do I get the row index or record Position of all items in gridData.SelectItems property.
SelectedItems Contains actual data (ViewModel) from the collection bound to datagrid's ItemSource. Therefore you can obtain the index in that collection for every item selected.
if its a datagridview on winforms then this would work
List<int> rowIndexs = new List<int>();
foreach (DataGridViewRow item in gridData.SelectedRows)
{
rowIndexs.Add(item.Index);
}

How to implement multiple column headers on ListView?

Like on the pic, there is second column header and two subheaders under it ?
For a complete example look at this link this should give you a full working example of what you can do
Add ListView Column and Insert Listview Items
try something like this
The order in which you add values to the array dictates the column they appear under so think of your sub item headings as [0],1 etc.
Here's a code sample:
//In this example an array of three items is added to a three column listview
string[] saLvwItem = new string[2];
foreach (string wholeitem in listofitems)
{
saLvwItem[0] = "Status Message";
saLvwItem[1] = wholeitem;
ListViewItem lvi = new ListViewItem(saLvwItem);
lvwMyListView.Items.Add(lvi);
}
To add SubItems you could also do something like this
lvwMyListView.Items[0].SubItems.Add("John Smith");

C# ListView Sub Items

I am trying to add items to a listView that has 2 columns. When I use the code below it will add an item to the first column and nothing to the second then it will create a new line with nothing in the first one and an item in the second, how do I get them both on the same line? Thanks.
listView1.Items.Add(item1);
ListViewItem date = new ListViewItem();
date.SubItems.Add(subitem1);
listView1.Items.Add(date);
This works:
ListViewItem item = new ListViewItem("some item");
item.SubItems.Add(new ListViewItem.ListViewSubItem(item, "sub item"));
this.listView1.Items.Add(item);
Set View property to the Details to check that everything right.
That's because you are adding two ListViewItems to the ListView. Make it look similar to this:
var item = new ListViewItem("first");
item.SubItems.Add("second");
item.SubItems.Add("third");
listView1.Items.Add(item);

Selecting ListView in C#

i have ListView with items
one ListView with items another one is empty
i need to copy from first ListView selected item to another ListView at the same time
i have to remove the selected item in first ListView in C#
Rather than removing items from the collection that you're enumerating (as per Wael's answer), which is a "bad idea", use a temporary collection, in this instance a List to store them in before removing them:
List<ListViewItem> itemsToMove = new List<ListViewItem>();
foreach (ListViewItem item in listView1.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
listView1.Items.Remove(item);
listView2.Items.Add(item);
}
Where listView1 is the list with the selected items and listView2 is the list to move them to.
this code will copy from the first listview1 to listview2 all the selected items and delete it from listview1
foreach (ListViewItem itm in ListView1.SelectedItems)
{
ListView1.Items.Remove(itm);
listView2.Items.Add(itm);
}

Categories

Resources