Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, February 9, 2013 12:00 AM
I am trying to add the contents of a list to a ListView.......I am reading a text file that has the info to fill the ListView....so as the user adds content to the text file the list will get larger....each line of the text file has 6 items....they are - the date,test result1 , test result2, test result3, test result4 and a note that may be added or not....the line of the text file is formatted like so....
date,result1,reult2,result3,result4,note
here is some code I wrote
private void frmViewResults_Load(object sender, EventArgs e)
{
// read the text file
StreamReader sr = new StreamReader(Application.StartupPath + "\\SugarData.txt");
string strLine = "";
// fill the list<string> with the contents of the text file
List<string> Numbers = new List<string>();
while (strLine != null)
{
char delimeter = (',');
strLine = sr.ReadLine();
string[] data = strLine.Split(delimeter);
for (int i = 0; i < data.Length; i++)
{
Numbers.Add(data[i]);
}
if (strLine != null)
{
ListViewItem lvi = new ListViewItem(Numbers[0]);
lvi.SubItems.Add(Numbers[1]);
lvi.SubItems.Add(Numbers[2]);
lvi.SubItems.Add(Numbers[3]);
lvi.SubItems.Add(Numbers[4]);
lvi.SubItems.Add(Numbers[5]);
LVResults.Items.Add(lvi);
}
}
sr.Close();
}
this code adds the same data in the rows...I have tried a loop but with no luck....
any help would be appreciated
thank you
InkedGFX
All replies (3)
Saturday, February 9, 2013 2:18 AM ✅Answered
Hi,
Can you try with the below code,
private void Form1_Load(object sender, EventArgs e)
{
LVResults.View = System.Windows.Forms.View.Details;
LVResults.GridLines = true;
LVResults.FullRowSelect = true;
// read the text file
StreamReader sr = new StreamReader("c:\\Test.txt");
string strLine = "";
// fill the list<string> with the contents of the text file
List<string> Numbers = new List<string>();
strLine = sr.ReadLine();
while (strLine != null)
{
char delimeter = (',');
string[] data = strLine.Split(delimeter);
for (int i = 0; i < data.Length; i++)
{
Numbers.Add(data[i]);
}
if (strLine != null)
{
ListViewItem lvi = new ListViewItem(Numbers[0]);
lvi.SubItems.Add(Numbers[1]);
lvi.SubItems.Add(Numbers[2]);
lvi.SubItems.Add(Numbers[3]);
lvi.SubItems.Add(Numbers[4]);
if(data.Length>5)
lvi.SubItems.Add(Numbers[5]);
else
lvi.SubItems.Add("");
LVResults.Items.Add(lvi);
}
strLine = sr.ReadLine();
}
sr.Close();
}
Best Regards
Muthuraja
Saturday, February 9, 2013 3:26 AM ✅Answered
You should:
1. read the file and add data to the list<T>
2. when adding into list<T> is compelte, add data to listview from the list<T>.
Example:
string filePath = "your file path";
using(StreamReader sr = new StreamReader(filePath))
{
string strLine = "";
List<string> data = new List<string>();
//reading file
while((strLine = sr.ReadLine())!= null)
{
data.Add(strLine);
}
//reading complete.
//now split data and add to listView:
foreach(string level1 in data)
{
string[] split = item.Split(',');
if(split.Length > 0)
{
ListViewItem lvi = new ListViewItem(split[0]);
for(int i = 1; i< split.Length;i++)
{
lvi.SubItems.Add(split[i]);
}
}
}
}//if using is used, no need to close the class, becuase using calls Dispose automatically when exiting the loop - this is here!
Mitja
Saturday, February 9, 2013 12:26 AM
To make us much easier, could you copy-paste your text file to pastebin and show us the results you get? And also to tell us what kind if result do you want to have.
Microsoft Student Parnter Microsoft Technology Associate