|
-
Oct 8th, 2006, 09:18 AM
#1
Thread Starter
Member
[Resolved] Listview Saving help! [VS2005]
Hi,
i am trying to get it so i can save the information from a listview to a textfile but i am stuck on how to get the 2 columns 1 being time and the other one being Events to write to the text file. Can anyone please help me this is how far i have got so far.
VB Code:
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
saveFileDialog1.Title = "Save Text File"; saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName);
file.WriteLine(listview1.Items);
file.Close();
}
}
Last edited by sk|Lance; Oct 9th, 2006 at 02:17 PM.
-
Oct 8th, 2006, 06:34 PM
#2
Re: Listview Saving help! [VS2005]
The Items property of a ListView is a collection of ListViewItems. The SubItems property of a ListViewItem is a collection of ListViewSubItems. Each subitem has a Text property. That's how you get the text displayed in a "cell" in a ListView. To visit every item in a collection you would use either a For or For Each loop. You have a collection of items and within each you have a collection of subitems. That means that you need two loops, one nested inside the other.
-
Oct 9th, 2006, 12:25 PM
#3
Thread Starter
Member
Re: Listview Saving help! [VS2005]
Hi,
Ive tryed sorta of how you recommended however i am missing something here and cant get it to work for both columns or all rows could you possbily help me?
VB Code:
System.IO.StreamWriter file = new System.IO.StreamWrite(saveFileDialog1.FileName);
for (int i = 0; i < this.lvLog.Items.Count; i++)
{
for (int j = 0; j < this.lvLog.Items[i].SubItems.Count; j++)
{
string tab = (j == 0) ? string.Empty : "\t";
file.WriteLine(lvLog.Items[i].SubItems[j].Text);
file.Close();
}
lvLog = the listview
-
Oct 9th, 2006, 01:16 PM
#4
Re: Listview Saving help! [VS2005]
You are checking for the TAB but you are not writing it
Code:
string tab = (j == 0) ? string.Empty : "\t";
file.WriteLine(lvLog.Items[i].SubItems[j].Text + tab);
Another 2 things:
1- you are not skipping a line when a row ends
2- You are closing the file after each subitem is written
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 9th, 2006, 01:28 PM
#5
Thread Starter
Member
Re: Listview Saving help! [VS2005]
i am not sure how to do the rest i am very new to c# and programming could you possibly help me? as for i am a noob and i cant currently think of how to do that.
-
Oct 9th, 2006, 01:47 PM
#6
Re: Listview Saving help! [VS2005]
you are not skipping a line when a row ends
put the following after the end of the for( j ) loop
You are closing the file after each subitem is written
Move the to the end (when you stop writing data) after closing the for ( i ) loop
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|