Hello,

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.

Recommended Answers

All 5 Replies

@GAME

Post your code here and someone will help you.

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
Mitja

Oh, I didn't notice that thanks.

You are welcome :)
bye,bye
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.