Prevent listview from adding blank subitem - c#

I have a listview in which the columns are dynamically created depending on the number of available subjects. When I create a new ListViewItem it automatically adds a blank subitem to the listview. Since the columns are dynamically created, the subitems are displaced. This is how I have done it.
foreach(int id in stud)
{
ListViewItem gtb = new ListViewItem();
foreach(int sname in subj)
{
gtb.SubItems.Add(value1);
gtb.SubItems.Add(value2);
gtb.SubItems.Add(value3);
}
gtb.SubItems.Add(value4);
listView1.Items.AddRange(new ListViewItem[] { gtb });
}
I know that adding the value1 to new ListViewItem(value1) would fix this but i have another subitem which is outside of the foreach loop. How can I fix this?

Add an 'if' statement that will check the length of each value then if the length is greater than zero you add the item to 'gtb'.

Create a String[], use it to collect your subitems, then create a new ListViewItem using that:
String[] subItems = new String[subj.Count + 1]; //add however many extras you need in place of 1
for (int j = 0; j < subj.Count; j++)
subItems[j] = subj[j];
//add whatever else to the array you want...
listView1.Items.Add(new ListViewItem(subItems));

I tried manually adding a new column which has a 0 width and it works just fine.

Related

Initializing ListView, why can't I do this?

As an old C++ programmer I am struggling with some C# issues. I want to reduce the redundancy in setting up a ListView. So I tried the code below. I get a null reference error, but I do not understand why. The compiler has no problem with me creating an array of ListViewItems, but I don't see how to use them.
Thanks, Russ
ListViewItem [] items = new ListViewItem [12];
for (int i=0; i < 12; ++i) {
items [i].Text = string.Format ("F{0}", i+1);
}
You're allocating memory for an Array of 12 ListViewItems, but you haven't created the ListViewItem:
Instead, try something like the following:
List<ListViewItem> items = new List<ListViewItem>();
for (int i = 0; i < 12; i++) {
items.Add( //using the List.Add() method to add an item
new ListViewItem
{
Text = string.Format ("F{0}", i+1); //Object Initialization syntax to add an item after construction
});
}
This will create a List (an expandable collection); create a new ListViewItem in a loop, give that ListViewItem the text you want it to have, and then add that item to the List.
It seems that you're receiving the null reference because you're trying to set a property(Text) on something that wasn't initialized (ListViewItem[i]).
Try this:
ListViewItem [] items = new ListViewItem [12];
for (var i = 0; i < items.Length; ++i) {
items[i] = new ListViewItem{Text = string.Format ("F{0}", i+1)};
}

How to Add Items From One ListView to Another

I am trying to parse the data in the first column of one ListView called raw and then if the data is correct, add that item to a second ListView called result.
However, when I go to run my program I get
the error:
"Cannot add or insert the item 'Collected' in more than one place.".
My Code
ListView result = new ListView();
for (int i = 0; i < raw.Items.Count; i++)
{
if (raw.Items[i].SubItems[0].Text.ToUpper() == "COLLECTED")
{
MessageBox.Show("confirm");
result.Items.Add(raw.Items[i]); // generating erros
}
}
printUsingLView(result);
clone original item:
result.Items.Add((ListViewItem)raw.Items[i].Clone());
or, if you want to make some adjustments
ListViewItem newItem = new ListViewItem();
newItem.Text = raw.Items[i].Text;
//enter other properties here and then add it to new listView
result.Items.Add(newItem);

Change column in ListView

My application contains ListView with files that i am handling and have 3 columns: file name, length and status.
Inside my for loop i am handling file after file and want in this case to change the status column from wait which is the value at the beginning to in process.
is it possible to change one column ?
lvFiles is my ListView
for (int i = 0; i < lvFiles.Items.Count; i++)
{
//here i am do things with my file
}
Here i am adding files into my ListView:
ListViewItem item = new ListViewItem(new string[] { new FileInfo(filePath).Name, duration, "Waiting" });
Use SubItems property of ListViewItem:
foreach(ListViewItem item in lvFiles.Items)
item.SubItems[2].Text = "Waiting";
You can try something like this if you know the specific Column for example Address would be
colString[2] you can do a single line
string[] colString = new string{ "Starting", "Paused", "Waiting" };
int colIndex = 0;
foreach (ColumnHeader lstViewCol in lvFiles.Columns)
{
lstViewCol.Text = colString[colIndex];
colIndex++;
}
for single column you stated you wanted the 3rd column then you could something like this
lvFiles.Colunns[2] = "waiting";

replacing an object in listview with new one?

I have a listview with few items. I am using foreach loop to check if there is a match. The code I am using looks like this:
foreach (ListViewItem test in listView1.Items)
{
if (test.SubItems[1].ToString() == item.SubItems[1].ToString())
{
test.Tag = item.Tag;
}
}
What I am trying to do is, check the 2nd index and if there is a match replace the old item 'test' with the new one 'item'.
Apparently there is no change in the listview. Is the way I am replacing the object wrong?
you can clone the item and assign directly to the list view item. but you need to change foreach loop to for loop.
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].SubItems[1].ToString() == item.SubItems[1].ToString())
{
listView1.Items[i] = (ListViewItem)item.Clone();
}
}
You have updated the Tag only. You need to change test.SubItems[0], test.SubItems[1],... to see the changes.
Or you could remove old item and insert new item by using listView1.Items.Remove(...) or listView1.Items.RemoveAt(...) and listView1.Items.Insert(...). But if you need to pay account of performance you should use the first algorithm (changing test.SubItems[i]).

Select the specific column of ListView and print it in a new messagebox in C#.net

I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}

Categories

Resources