我需要将所有数据从 a 传递DataGridView
到 MySQL 中的数据库,唯一的选择是循环DataGrid
并插入。
我的旧代码是:
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());
}
但它不适用于我想要的,或者它是错误的。
如何遍历行DataGrid
并从每个行中获取数据?
我的数据网格
解决方案
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);
}
}
DataGridView
您可以通过以下方式进行迭代:更新
使用列名编辑代码示例。