Error entering data to a Datagridview in C #

0

I have a DataGridView that has two functions depending on whether you select the Agregar or Modificar button:

This is the DataGridView :

If I do Click to the button Agregar of the first image without having previously selected the button Modificar , if you let me pass the data of the first DataGridView to the second DataGridView , otherwise, if you select previously the Modificar button and then the Agregar button I get the following error:

This is the code that has the Agregar button of the second image where the two DataGridView are (if you select the Agregar button of the first image):

foreach (DataGridViewRow row in dgvInsumo.Rows)
                {

                    if (Convert.ToBoolean(row.Cells[0].Value))
                    {
                        bool exist = dgvInsumosAgregados.Rows.Cast<DataGridViewRow>().Any(row2 => row2.Cells["nombre"].Value == row.Cells[2].Value);

                        if (!exist)
                        {
                            DataGridViewTextBoxColumn filaTxb = new DataGridViewTextBoxColumn();

                            DataGridViewRow fila = new DataGridViewRow();
                            fila.CreateCells(dgvInsumosAgregados);
                            fila.Cells[0].Value = row.Cells[1].Value;
                            fila.Cells[1].Value = row.Cells[2].Value;
                            fila.Cells[2].Value = row.Cells[4].Value;
                            fila.Cells[3].Value = row.Cells[3].Value;
                            fila.Cells[4].Value = 0;
                            fila.Cells[5].Value = 0;
                            dgvInsumosAgregados.Rows.Add(fila);
                        }
                    }
                }

Needless to say, this method does not connect to MySQL or execute any Procedimiento Almacenado until you select the Save button. When modifying a saucer, it does not create any problem just when adding. I already tried everything that is within my reach but I could not get it, I hope you can help me, thank you.

    
asked by U.C 11.08.2017 в 23:00
source

0 answers