Friday, 6 September 2013

Adding to columns based on length

Adding to columns based on length

I have a ListView with two columns, Boxes and Files. I'm adding items to a
list of strings, and then populating the ListView with that list of
strings. I want to make it so all items that are 8 characters long go into
the Boxes column and all items that are 9 characters go into the Files
column. So far, I've tried to iterate through using a for loop and utilize
an if else statement to add the items, but I seem to be doing something
wrong. Here's my current code:
public void PopulateItemsList()
{
BoxAndFileList.Items.Clear();
ScanIdBox.Text = string.Empty;
for (int i = 0; i < BoxNumberRepository._boxAndFileList.Count; i++)
{
var item = BoxNumberRepository._boxAndFileList.Item[i];
if (item.Length == 8)
{
BoxAndFileList.Items.Insert(0, item);
}
else
{
BoxAndFileList.Items.Insert(1, item);
}
}
}
I'm iterating through my list (_boxAndFileList) and trying to utilize
Insert() to insert items into the specific index of the columns (Boxes is
0, Files is 1). I can clearly see that Item is a legitimate property of a
string list, yet VS keeps saying that list contains no definition of it.
How can I go about doing this? And also, I haven't received outside
feedback on this way of doing things yet, so if there's a better way,
please let me know.
Edit: BoxNumberRepository is a class that news up a list called
_boxAndFileList. Code below:
public class BoxNumberRepository : Scan_Form
{
public static List<string> _boxAndFileList = new List<string>();
public void AddItem(string item)
{
_boxAndFileList.Add(item);
}
public void Delete(string item)
{
_boxAndFileList.Remove(item);
}
public IEnumerable<string> GetAllItems()
{
return _boxAndFileList;
}
}
Thanks to Alessandro D'Andria for that suggestion. That was correct.
However, all the items are still just adding to the first column, even if
they're 9 characters. How can I get 9 character items to add to the second
column?

No comments:

Post a Comment