1

我有一个包含两列 Boxes 和 Files 的 ListView。我将项目添加到字符串列表中,然后使用该字符串列表填充 ListView。我想让所有 8 个字符长的项目进入 Boxes 列,所有 9 个字符的项目进入 Files 列。到目前为止,我已经尝试使用 for 循环进行迭代并使用 if else 语句来添加项目,但我似乎做错了什么。这是我当前的代码:

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);
            }
        }
    }

我正在遍历我的列表(_boxAndFileList)并尝试利用 Insert() 将项目插入列的特定索引(框为 0,文件为 1)。我可以清楚地看到 Item 是字符串列表的合法属性,但 VS 一直说该列表不包含它的定义。我该怎么做呢?而且,我还没有收到外界对这种做事方式的反馈,所以如果有更好的方法,请告诉我。

编辑:BoxNumberRepository 是一个更新名为 _boxAndFileList 的列表的类。下面的代码:

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;
        }
    }

感谢 Alessandro D'Andria 的建议。那是正确的。但是,所有项目仍然只是添加到第一列,即使它们是 9 个字符。如何将 9 个字符项添加到第二列?

4

2 回答 2

1

您遇到的问题是您必须同时添加box和。filelist item

编辑:将笛卡尔积更改为左外连接。

编辑:添加了注释并修复了一个语法错误

private List<string> _boxAndFileList = new List<string> { "12345678", "123456789", "1234", "123456778" };
public void PopulateItemsList()
{
    //clear the list
    BoxAndFileList.Items.Clear();
    //add the labels to the top of the listbox
    BoxAndFileList.Columns.Add("Boxes");
    BoxAndFileList.Columns.Add("Files");
    //set the view of the list to a details view (important if you try to display images)
    BoxAndFileList.View = View.Details;
    //clear scan id box
    ScanIdBox.Text = string.Empty;

    //get all the items whos length are 8 as well as a unique id (index)
    var boxes = _boxAndFileList.Where(b => b.Length == 8).Select((b, index) => new { index, b }).ToList();
    //get all the items whos length are NOT 8 as well as a unique id (index)
    var files = _boxAndFileList.Where(f => f.Length != 8).Select((f, index) => new { index, f }).ToList();

    //join them together on their unique ids so that you get info on both sides.
    var interim = (from f in files
                   join b in boxes on f.index equals b.index into bf
                   from x in bf.DefaultIfEmpty()
                   select new { box = (x == null ? String.Empty : x.b), file = f.f });
    //the real trick here is that you have to add 
    //to the listviewitem of type string[] in order to populate the second, third, or more column.
    //I'm just doing this in linq, but var x = new ListViewItem(new[]{"myBox", "myFile"}) would work the same
    var fileboxes = interim.Select(x => new ListViewItem(new []{ x.box, x.file})).ToArray();
    //add the array to the listbox
    BoxAndFileList.Items.AddRange(fileboxes);
    //refresh the listbox
    BoxAndFileList.Refresh();
}
于 2013-09-06T22:51:02.427 回答
0

您的 _boxAndFileList 是一个List<string>,因此您应该将项目声明为string类型而不是var类型:

string item = BoxNumberRepository._boxAndFileList.Item[i];

你所有的代码应该是这样的:

public void PopulateItemsList()
{
    BoxAndFileList.Items.Clear();
    ScanIdBox.Text = string.Empty;
    for (int i = 0; i < BoxNumberRepository._boxAndFileList.Count; i++)
    {
        string item = BoxNumberRepository._boxAndFileList.Item[i];
        if (item.Length == 8)
        {
            BoxAndFileList.Items.Insert(0, item);
        }
        else
        {
            BoxAndFileList.Items.Insert(1, item);
        }
    }
}
于 2013-09-06T21:35:47.453 回答