Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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
Posted
Updated 2-Dec-14 2:52am
v3
Comments
BillWoodruff 1-Dec-14 19:28pm    
RyanDev has shown you the problem in your code.

I'd like to suggest that for questions like this you just post the code in your question: the code is not "huge," and it's distracting to have to open a web-page to go study a picture to figure out what's going on, in fact I predict many people here will just skip considering your question because of the link to an external source.
Avenger1 2-Dec-14 8:53am    
i fixed my question
i didn't understand what to do in that solution
please help me to fix it
With Respect
BillWoodruff 2-Dec-14 21:39pm    
Wasn't this question already asked and answered in another thread ?
syed shanu 2-Dec-14 21:38pm    
HI,
Do you want to display both b.bat and a.bat file lines to display in 2 separate column of list view for example first column display b.bat file line and 2nd column of listview display a.bat of line.
If you tell more clearly your requirement it will be easy to tell the solution.

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.
 
Share this answer
 
Comments
Avenger1 1-Dec-14 11:35am    
i did it and it was normal but as you see it add just the last subItem
please help me to fix it
With Respect
ZurdoDev 1-Dec-14 11:53am    
Of course it only adds the last subitem. Where exactly are you stuck then? I already explained how to fix it.
BillWoodruff 1-Dec-14 19:28pm    
+5
you need loop inside loop or use LINQ to build the data you need from two files. for example :
C#
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
C#
listView1.Items.AddRange(lines2.Zip(lines, (a, b) => new ListViewItem( new [] { a, b })).ToArray());
 
Share this answer
 
v3
Comments
Avenger1 2-Dec-14 17:35pm    
this code showed but for example if i had two items and two subitems it showed me 4 items and 4 subitems
what should i do?
With Respect
DamithSL 2-Dec-14 20:20pm    
check my updated answer
BillWoodruff 2-Dec-14 21:38pm    
That's very interesting code; I am trying to understand how that handles both adding ListViewItems and SubItems. thanks, Bill
DamithSL 2-Dec-14 22:19pm    
Is it? loop will do the same but again it will be doing the homework as suggested in 1st answer. that's why I have post LINQ solution with combining two arrays and generate the array of list items. Hope this will help someone else in future :) (at least check what is Enumerable.Zip method)
BillWoodruff 2-Dec-14 22:59pm    
using your code with this test data:

private string[] items = new string[4] {"1", "2", "3", "4"};
private string[] subItems = new string[4] {"a", "b", "c", "d"};

the ListView shows only values in the first (Item) and second (SubItem) columns: no SubItems shown in 3rd. to 5th. columns.
C#
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).
 
Share this answer
 
Comments
Avenger1 2-Dec-14 23:45pm    
thanks bill but your answer just add first subitem and the second Solution was the great answer
With Respect
BillWoodruff 3-Dec-14 0:14am    
Hey, I'm glad you got a userful answer; my interpretation of what you were after was something that given:

private string[] items = new string[4] {"1", "2", "3", "4"};
private string[] subItems = new string[4] {"a", "b", "c", "d"};

produced:

1 a b c d
2 a b c d
3 a b c d
4 a b c d

:)

If you are simply getting one SubItem per row of output, you do not need a loop inside a loop !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900