I need to pass all the data from a DataGridView
to a database in MySQL, the only option to do is loop through DataGrid
and insert.
My old 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 doesn't work for what I want or it's wrong.
How can I iterate through the row DataGrid
and get the data from each of them?
mydatagrid
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);
}
}
You can iterate
DataGridView
in the following way:Update
Code example is edited with the name of the columns.