Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How do I save selected lines from a ListView to a text file?

What I have tried:

I tried using this;

<pre lang="c#">
        private void CMSavetoFile_Click(object sender, EventArgs e)
        {
            foreach (var v in LV.SelectedItems)
            {
                File.AppendAllText("outputfile.txt", v.ToString() + Environment.NewLine);
            }
        }


This only returns column one and I have a total of 12 columns (0-11).

I find lots of examples of saving the entire ListView to a text file or excel, etc, but not just the selected items.

Thanks!
Posted
Updated 7-Oct-19 8:24am
Comments
phil.o 7-Oct-19 12:59pm    
Are you able to enumerate each column for a specific row?
solutionsville 7-Oct-19 14:28pm    
This is what i use to display data in the list view;

private void UpdateListview()
{
ListView TLV;
TLV = FilterView ? LV2 : LV;
List<griditem> list = new List<griditem>(GridList);
TLV.UseWaitCursor = true;
TLV.Items.Clear();
TLV.BeginUpdate();

// blank the grid, and show the little centered progress bar
JPanel.Top = TLV.Top + TLV.Height / 2;
JPanel.Left = TLV.Left + TLV.Width / 2 - JPanel.Width / 2;
JPB.Maximum = list.Count;
JPB.Value = 0;
JPanel.Visible = true;
JPanel.BringToFront();

// write only filtered GridLines to the grid
foreach (var s in list)
{
if (s.show == false) continue;
var t = new ListViewItem(s.Time)
{
Tag = s
};
t.SubItems.Add(s.Line.ToString());
t.SubItems.Add(s.Group.ToString());
t.SubItems.Add(s.Label);
t.SubItems.Add(s.HexLabelStr);
t.SubItems.Add(s.dir);
t.SubItems.Add(s.Tseq);
t.SubItems.Add(s.Rseq);
t.SubItems.Add(s.Mnum);
t.SubItems.Add(s.ip);
t.SubItems.Add(Utils.PbaseToStr(s.pbase));
t.SubItems.Add(s.payload);
TLV.Items.Add(t);
JPB.Value++;
}
TLV.EndUpdate();
TLV.UseWaitCursor = false;
JPanel.SendToBack();
JPanel.Visible = false;
SelectedGridItem = null;
}
phil.o 7-Oct-19 14:38pm    
Sorry, but that is not an answer to my question :)
solutionsville 11-Oct-19 10:37am    
My Apologies. I did not understand your question. This is what i am using to save the enter content of the ListView to a file.

private async void CMSavetoFile_Click(object sender, EventArgs e)
{
try
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "OCG Dat File | *.dat", ValidateNames = true, FileName = @"C:\Test.dat", OverwritePrompt = false })
{
if (sfd.ShowDialog() == DialogResult.OK)
{
using (TextWriter tw = new StreamWriter(new FileStream(sfd.FileName, FileMode.Create), Encoding.UTF8))
{
foreach (ListViewItem item in LV.Items)
{
await tw.WriteLineAsync(item.SubItems[0].Text + "\t" + item.SubItems[1].Text + "\t" + item.SubItems[2] + "\t" + item.SubItems[3] + "\t" + item.SubItems[4] + "\t" + item.SubItems[5] + "\t" + item.SubItems[6] + "\t" + item.SubItems[7] + "\t" + item.SubItems[8] + "\t" + item.SubItems[9] + "\t" + item.SubItems[10] + "\t" + item.SubItems[11]);
}
MessageBox.Show("File Created!");
}
}
}
}

catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

1 solution

Your need to add a ToString() override to your list items. This overridden method should include a call to either string.Format, or use the StringBuilder class to construct your result in a formatted way.
 
Share this answer
 
Comments
solutionsville 8-Oct-19 9:49am    
Add it to where?
#realJSOP 8-Oct-19 10:01am    
The item class that represents your grid items.

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