It lacked to give more information and put the code you have tried. Supposing that the bank is the SQlServer
, and your grid has 4 columns, would be like this:
I don’t know what kind of data you use, so stick to the fields. The contents of the cells will have the return type object
.
void AddPedidos()
{
string sqlQuery;
string strConexao = "Seu caminho para o banco";
try
{
SqlConnection Conexao = new SqlConnection(strConexao);
Conexao.Open();
foreach (DataGridViewRow linha in dgvProdutos.Rows)
{
qlQuery = $"INSERT INTO Pedidos(ID, Emissao, ProdutoID, Quantidade) VALUES({linha.Cells[0].Value}, {linha.Cells[2].Value}, {linha.Cells[3].Value}, {linha.Cells[4].Value})";
SqlCommand Comando = new SqlCommand(sqlQuery, Conexao);
Comando.ExecuteNonQuery();
}
Conexao.Close();
}
catch (SqlException erro)
{
MessageBox.Show($"Ocorreu um erro: {erro.Message}");
}
}
You can use the ForEach
to traverse all lines on the Grid.
The dgvProdutos
is the Datagridview.
put the code you already have ready
– Rovann Linhalis