Prevent listview from adding blank subitem

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?

Answers


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.


Need Your Help

Pandas and unicode

python unicode pandas

This is a string I'm getting out of pandas.DataFrame.to_json(), putting it into redis, getting it out of redis elsewhere, and trying to read it via pandas.read_json():