Save a datagrid column in the database

Asked

Viewed 44 times

0

I need to get the value of the discount column from mine datagrid and save in my database, will I get, it is calculated when I record consumption according to quantity, follows below the code:

inserir a descrição da imagem aqui

foreach (var leitura in leituras)
{
    totalConsumo += leitura.Consumo;
    totalDesconto += leitura.Desconto;
}

dgv[0, dgv.Rows.Count - 1].Value = "Total";
dgv[1, dgv.Rows.Count - 1].Value = totalConsumo.ToString();
dgv[2, dgv.Rows.Count - 1].Value = totalDesconto.ToString();
lblResultado.Text = "Total consumo com desconto: " + (totalConsumo - totalDesconto).ToString();

Persistence code

    CONEXAO.Open(); 
    MySqlCommand INSERT = new MySqlCommand("INSERT INTO consumo (casa, consumo)" + "VALUES ('" + casa + "','" + consumo + "')", CONEXAO);
    INSERT.Parameters.AddWithValue(casa, txtCasa.Text); 
    INSERT.Parameters.AddWithValue(consumo, txtConsumo.Text); 
    INSERT.ExecuteNonQuery(); 
    MessageBox.Show("O registro foi inserido com sucesso !");
  • If the discount comes according to consumption record, just take the final value you have in the total control variable Note and add it to the object of the class you want to save.

  • The same logical pattern that is used to popular the object can be used to send the data to the database. The structure of repetition and access by index.

  • @Diogenes Edit your question to include the code in it, not in the comments.

  • @Leandroangelo, thanks for the tip !

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.