I need some help getting started with saving all the items in a listview with a savefiledailog. The items in the list look like this IP , port. The IP and port are in different columns. The IP is in the first column and the IP is in the 2nd.

7 Years
Discussion Span
Last Post by Mitja Bonca
for (int i = 0; i < listView1.Items.Count; i++)
                write += listView1.SelectedItems[i].Text + ":" + listView1.SelectedItems[i].SubItems[1].Text;
                write += Environment.NewLine;
            Stream myStream; 
            SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
            if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
                if ((myStream = saveFileDialog1.OpenFile()) != null) 
                    StreamWriter wText = new StreamWriter(myStream); 
                    wText.Write(write); myStream.Close(); 

When I start the code I get an error saying
InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index

Pointing to write += listView1.SelectedItems.Text + ":" + listView1.SelectedItems.SubItems[1].Text;


You said you want to save all items from a listView, not SelectedItem. So why are you using Selecteditem property?

What you hav to do it to loop through all the rows and get the values from all the columns (while in a particualar row), like this:

  string text = null;
  for (int i = 0; i < listView1.Items.Count; i++)
       text += listView1.Items[i].Text + ": " + listView1.Items[i].SubItems[1].Text + Environment.NewLine;    

This should do the trick

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.