Hi I recently picked up C# and have gone through a couple tutorials but I definitely still have a lot to learn especially by doing, so apologies in advance if I have set this up incorrectly or am not going about this an efficient way.
嗨,我最近选择了C#并且已经完成了几个教程,但我还是有很多需要学习的东西,所以如果我错误地设置了这个或者不是这个有效的方法,那么请提前道歉。
So as the title states, I am trying to import a list with columns into a listview. More specifically, a class with strings into a listview. (I am still new to all this so let me know if there is a better way of going about this.) I think I know how to manually add listview items to columns based on this post C# listView, how do I add items to columns 2, 3 and 4 etc? What I have now is using lstViewPrinters.Items.Add(_printerlist[i].ToString());
but this adds the whole class "printer" as a single listview item into a single column. I know I can access individual strings via _printerlist[i].Hostname.ToString();
as well. The relevant layout of my class is shown below.
因此,正如标题所述,我正在尝试将列表导入列表视图。更具体地说,是一个将字符串放入listview的类。 (我还是对这一切都不熟悉所以请告诉我是否有更好的方法来解决这个问题。)我想我知道如何根据这篇文章C#listView手动将listview项添加到列中,如何向列添加项目2,3和4等?我现在拥有的是使用lstViewPrinters.Items.Add(_printerlist [i] .ToString());但是这会将整个类“printer”作为单个listview项添加到单个列中。我知道我可以通过_printerlist [i] .Hostname.ToString();访问单个字符串。同样。我班级的相关布局如下所示。
List<Printer> _printerlist = new List<Printer>();
public class Printer
{
public string Hostname { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
public Printer() // this is a method within the Printer class
{
Hostname = string.Empty;
Manufacturer = string.Empty;
Model = string.Empty;
}
}
I'm getting really close with this short code snippet below, but I need to be able to add 2 items.
我正在接近下面的这个简短的代码片段,但我需要能够添加2个项目。
for(int i=0; i<_printerlist.Count; i++)
{lstViewPrinters.Items.Add(_printerlist[i].Hostname).SubItems.Add(_printerlist[i].Manufacturer);}
Is the best method to go about this to just make it a range and delete the doubled up column? Another method I saw was to add items with the item1.SubItems.Add("SubItem1a");
command but I have my system in a for
loop so I can't do that (or at least I don't know how, if someone can direct me to declaring ListViewItem item1 = new ListViewItem("Something");
in a loop with changing names (item1) I'd be grateful as well.)
是最好的方法来使它成为一个范围并删除加倍的列?我看到的另一种方法是使用item1.SubItems.Add(“SubItem1a”)添加项目;命令,但我的系统在for循环中,所以我不能这样做(或者至少我不知道如何,如果有人可以指示我声明ListViewItem item1 = new ListViewItem(“Something”);在循环中改变名字(第1项)我也很感激。)
Could I get suggestions as to how I can add the class/list straight to the listview? Or how I should restructure my class, if that's a better solution. Any general naming convention notes as well as links to other helpful links would be appreciated as well.
Thanks.
我可以获得有关如何将类/列表直接添加到列表视图的建议吗?或者我应该如何重组我的班级,如果这是一个更好的解决方案。任何一般命名约定注释以及指向其他有用链接的链接也将受到赞赏。谢谢。
1 个解决方案
#1
0
ListViewItem has a bunch of constructors, you can add all the properties in the new statement like this
ListViewItem有一堆构造函数,你可以像这样添加新语句中的所有属性
var _printerlist = new List<Printer>();
for (int i = 0; i < _printerlist.Count; i++)
{
lstViewPrinters.Items.Add(
new ListViewItem(new[]
{
_printerlist[i].Hostname,
_printerlist[i].Manufacturer,
_printerlist[i].Model
}));
}
Or for fun, you can do the entire thing in one statement with LINQ
或者为了好玩,您可以使用LINQ在一个语句中完成整个过程
_printerlist.ForEach(p => lstViewPrinters.Items.Add(
new ListViewItem(new[]
{
p.Hostname,
p.Manufacturer,
p.Model
})));
#1
0
ListViewItem has a bunch of constructors, you can add all the properties in the new statement like this
ListViewItem有一堆构造函数,你可以像这样添加新语句中的所有属性
var _printerlist = new List<Printer>();
for (int i = 0; i < _printerlist.Count; i++)
{
lstViewPrinters.Items.Add(
new ListViewItem(new[]
{
_printerlist[i].Hostname,
_printerlist[i].Manufacturer,
_printerlist[i].Model
}));
}
Or for fun, you can do the entire thing in one statement with LINQ
或者为了好玩,您可以使用LINQ在一个语句中完成整个过程
_printerlist.ForEach(p => lstViewPrinters.Items.Add(
new ListViewItem(new[]
{
p.Hostname,
p.Manufacturer,
p.Model
})));