将数据加载到listview的问题 [英] problem with load data to listview

查看:78
本文介绍了将数据加载到listview的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i想要将数据加载到我的listView中,当我这样做时它没有项目问题但是对于子项目它只添加文本文件中的最后一个单词

有人可以告诉我该怎么办?

我的问题图片 [ ^ ]

,这是我的代码

  private   void  LoadData(ListView slv )
{
string [] lines2 = File.ReadAllLines( b.bat);
foreach string line2 in lines2)
{
b = line2 + \ n;
}
string [] lines = File.ReadAllLines( a.bat);
foreach string line in 行)
{
a = line + \ n;
ListViewItem item = new ListViewItem(a);
item.SubItems.Add(b);
slv.Items.AddRange( new ListViewItem [] {item});
}
}



i对项目没有问题,但对于subItems它只是添加最后一个

尊重

解决方案

如果你调试它,你会看到到底发生了什么。你是第一次循环运行,它所做的只是为b赋值。然后它使用现在在b中的值进行另一个循环,这当然是最后一个值。



看起来你想要做一个嵌套循环但是在不确定你想要什么的情况下,很难肯定地说。


你需要循环内部循环或使用LINQ从两个文件构建你需要的数据。例如:

  string  [] lines2 = File.ReadAllLines(  b.bat); 
string [] lines = File.ReadAllLines( a.bat);
if (lines2.Length == lines.Length)
{
var res = lines2.Zip(lines,(a,b)= > new string [] {a,b});
slv.Items.AddRange(res.Select(r = > new ListViewItem(r ))ToArray的())。
}







 listView1.Items.AddRange (lines2.Zip(行,(a,b)= >   new  ListViewItem( new  [] {a,b}))。ToArray()); 


 私有  void  LoadData(ListView slv)
{
string [] subItems = File.ReadAllLines( b.bat);

string [] items = File.ReadAllLines( a.bat);

foreach string item in items)
{
ListViewItem lvItem = new ListViewItem(item);

foreach string subItem in subItems)
{
lvItem.SubItems.Add( new ListViewItem.ListViewSubItem(lvItem,subItem));
}

slv.Items.Add(lvItem);
}
}

请记住,您必须在构建/添加ListViewItem的循环中添加ListViewItem.ListViewSubItem。


Hi i want to load data to my listView and when i do it it has no problem with items but for subitems it add just the last word from the text file
can someone tell me what should i do?
My Problem Picture[^]
and here is my code

private void LoadData(ListView slv)
{
    string[] lines2 = File.ReadAllLines("b.bat");
    foreach (string line2 in lines2)
    {
        b = line2 + "\n";
    }
    string[] lines = File.ReadAllLines("a.bat");
    foreach (string line in lines)
    {
        a = line + "\n";
        ListViewItem item = new ListViewItem(a);
        item.SubItems.Add(b);
        slv.Items.AddRange(new ListViewItem[] { item });
    }
}


i doing without problem for items but for subItems it just add the last one
With Respect

解决方案

If you debugged it you would see exactly what is happening. You're first loop runs which all it does is assign a value to b. It then does another loop using the value which is now in b which is of course the last value.

It looks like you'll want to do a nested loop but without knowing for sure what you want, it's hard to say for sure.


you need loop inside loop or use LINQ to build the data you need from two files. for example :

string[] lines2  = File.ReadAllLines("b.bat");
string[] lines  = File.ReadAllLines("a.bat");
if(lines2.Length == lines.Length)
{
    var res = lines2.Zip(lines, ( a,b) => new string[] { a, b});
    slv.Items.AddRange(res.Select(r=> new ListViewItem(r)).ToArray());
}



OR

listView1.Items.AddRange(lines2.Zip(lines, (a, b) => new ListViewItem( new [] { a, b })).ToArray());


private void LoadData(ListView slv)
{
    string[] subItems = File.ReadAllLines("b.bat");

    string[] items = File.ReadAllLines("a.bat");

    foreach (string item in items)
    {
        ListViewItem lvItem = new ListViewItem(item);

        foreach (string subItem in subItems)
        {
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem,subItem));
        }

        slv.Items.Add(lvItem);
    }
}

Just keep in mind that that you must add the ListViewItem.ListViewSubItem(s) inside the loop where you build/add the ListViewItem(s).


这篇关于将数据加载到listview的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆