0

I am currently working on a weather forecasting application which stores forecasts in a textfile. The functionality of capturing the information into a textfile has been complete. I have also displayed the information in a DataGridView. The issue I have encountered is when a change is made to the DataGridView and the "Save" button is clicked, the updated information must be written to a textfile.

I keep getting a ArgumentOutOfRangeException error on the windspeed1 = dgRow.Cells[8].Value.ToString();

string city1, day, date1, minTemp1, maxTemp1, precipitation1, humidity1, windSpeed1;

foreach (DataGridViewRow dgRow in dataGridView2.Rows)
{

    city1 = dgRow.Cells[1].Value.ToString();
    day = dgRow.Cells[2].Value.ToString();
    date1 = dgRow.Cells[3].Value.ToString();
    minTemp1 = dgRow.Cells[4].Value.ToString();
    maxTemp1 = dgRow.Cells[5].Value.ToString();
    precipitation1 = dgRow.Cells[6].Value.ToString();
    humidity1 = dgRow.Cells[7].Value.ToString();
    windSpeed1 = dgRow.Cells[8].Value.ToString();


    using (StreamWriter sw = new StreamWriter("forecasts.txt", true))
    {
      sw.WriteLine(city1 + "," + day + "," + date1 + "," + minTemp1 + "," + 
      maxTemp1 + "," + precipitation1 + "," + humidity1 + "," + windSpeed1);

      sw.Close();
    }
}
Faheem
  • 1
  • Array indices are zero-based. I assume City is zero, not one, the rest follow. – Crowcoder May 12 '19 at 00:41
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Jimi May 12 '19 at 02:00

0 Answers0