You have to give more information and put the code that you have tried. Assuming the bank is SQlServer
, and your grid has 4 columns, it would look like this:
I do not know what kind of data you're using, so watch out for 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 ForEach
to traverse all rows in the grid.
The dgvProdutos
is the DataGridView.