Mehmet Fatih

Mehmet Fatih

  • 746
  • 904
  • 30.8k

Index out Range error

Sep 16 2023 9:18 AM

I use progressbar in my form. I add "Application.DoEvents()" so that the progress bar fires when I click the button. However, in this case, I get the "index out of range" error. What do you think about how we can fix the error?

        System.Threading.Thread.Sleep(10);

        this.backgroundWorker1.RunWorkerAsync();
        backgroundWorker1.WorkerReportsProgress = true;
        Cursor.Current = Cursors.WaitCursor;
        progressBar1.Show();
        label1.Show();
        pictureBox1.Show();
        progressBar1.Value = 0;
 

for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
    Application.DoEvents();
    progressBar1.PerformStep();

    var val0 = dataGridView1.Rows[i].Cells[0].Value.ToString(); 
        var val1 = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
        var val2 = dataGridView1.Rows[i].Cells[2].Value.ToString(); 
        var val3 = dataGridView1.Rows[i].Cells[3].Value.ToString(); 
        var val4 = dataGridView1.Rows[i].Cells[4].Value.ToString(); /
        var val5 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[5].Value.ToString()); 
        var val6 = dataGridView1.Rows[i].Cells[6].Value.ToString();  
        var val7 = dataGridView1.Rows[i].Cells[7].Value.ToString();  
        var val8 = dataGridView1.Rows[i].Cells[8].Value.ToString();  


Answers (14)