我有一个包含两列BoxView和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()将项插入列的特定索引(Boxes为0,Files为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个字符项添加到第二列?
答案 0 :(得分:1)
您遇到的问题是您必须同时将box
和file
添加到list 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();
}
答案 1 :(得分: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);
}
}
}