I have a problem 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll in C #

0

I must print each of the elements that I bring to a GRID but it generates that error when entering the cycle if I put it in in try to print them but the program does not close it is executed in the background and I must close it killing the process

 private void button2_Click_1(object sender, EventArgs e)
    {
        r = new imprimirmanifiesto();
        int pos = 0;
        bool a = true;
        while (a == true)
        {
// EL ERROR SALE EN ESTA LINEA
            if (string.IsNullOrEmpty(dataGridView1.Rows[pos].Cells[0].Value.ToString()))
///----
            {
                a = false;
            }
            else
            {
                a = true;
            }
            r.imprimir(dataGridView1.Rows[pos].Cells[1].Value.ToString());
            pos++;
        }
    }
    
asked by Saul Salazar 01.08.2018 в 00:39
source

2 answers

0

I suggest you modify the way you access the datagrid records.

You can use a for cycle considering the number of records the datagrid has, for example:

for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
    r.imprimir(dataGridView1.Rows[pos].Cells[1].Value.ToString());
}
    
answered by 01.08.2018 / 00:54
source
0

You could add another condition to your while like this:

while (a == true && pos < dataGridView1.Rows.Count)
    
answered by 01.08.2018 в 00:44