Touring a DataGridView in C #

1

I need to pass all the data from a DataGridView to a database in MySQL, the only option to do is going through DataGrid and inserting.

My previous code was:

foreach (DataGridViewRow dgvRenglon in dtaPagos.Rows)
{
    MessageBox.Show(dgvRenglon.Cells[0].Value.ToString());
    MessageBox.Show(dgvRenglon.Cells[1].Value.ToString());
    MessageBox.Show(dgvRenglon.Cells[2].Value.ToString());
}

But it does not work for what I want or is wrong.

How can I go through the row of DataGrid and get the data of each of them?

  

My DataGrid

  

Solution

for (int fila = 0; fila < dtaPagos.Rows.Count-1; fila++)
            {
                for (int col = 0; col < dtaPagos.Rows[fila].Cells.Count; col++)
                {
                    string valor = dtaPagos.Rows[fila].Cells[col].Value.ToString();
                    MessageBox.Show(valor);
                }
            }
    
asked by DoubleM 09.02.2017 в 05:05
source

1 answer

2

You can retrieve your DataGridView in the following way:

foreach (DataGridViewRow row in dtaPagos.Rows)
{
    MessageBox.Show(row.Cells["Pago"].Value.ToString());
    MessageBox.Show(row.Cells["Cantidad"].Value.ToString());
    MessageBox.Show(row.Cells["Observaciones"].Value.ToString());
}

Update

Code example is edited with the name of the columns.

    
answered by 09.02.2017 в 05:32