Select line in datagridwier and write to the database

Asked

Viewed 67 times

0

I have a datagridviwer populated by a query of the sql database, in this datagrid I have a column with a chekbox, now comes my doubt, as I write to the database when the line has been selected by the user in chekbox.

follow my datagrid screen inserir a descrição da imagem aqui

code today where I do the Insert in the bank but without chekbox working

private void ncheklist()
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = @"INSERT INTO TBL_CHEKLIST (N_NOTA, CLIENTE, TRANSP, VOLUME, N_CHEKLIST, EMISSAO) 
                      VALUES (@NOTA, @CLIENTE, @TRANSP, @VOLUME, @CHEKLIST, @EMISSAO)";
    cmd.Connection = conex1;
    conex1.Open();

    cmd.Parameters.Add(new SqlParameter("@NOTA", this.txt_nota.Text));
    cmd.Parameters.Add(new SqlParameter("@CLIENTE", this.txt_cliente.Text));
    cmd.Parameters.Add(new SqlParameter("@TRANSP", this.txt_transp.Text));
    cmd.Parameters.Add(new SqlParameter("@VOLUME", this.txt_volume.Text));
    cmd.Parameters.Add(new SqlParameter("@CHEKLIST", this.txt_nchklist.Text));
    cmd.Parameters.Add(new SqlParameter("@EMISSAO", Convert.ToDateTime(txt_dtinicial.Text).ToString("yyyy/MM/dd").Replace("/", "")));

    cmd.ExecuteNonQuery();
    conex1.Close();
}

2 answers

0


To check if the CHECKBOX column is checked I do the following way:

// varrendo o dataGrid
for (Int32 index = 0; index < meuGrid.Rows.Count; index++)
{
   //verifica se a linha do grid está com o checkbox ativado
   //cells[0] = corresponde a primeira coluna, ou seja a que possui o CHECKBOX
   if (bool.Parse(meuGrid.Rows[index].Cells[0].FormattedValue.ToString()) == true)
   {
      // executa ação de inserção no banco de dados
   }
}
  • vnbrs, thanks for editing, where I click to edit to c# usually put between the tags "code".

  • Thank you so much for your attention, I tested it here and it worked, I just couldn’t do take the value of the columns and do the Insert, I mean I couldn’t do for hi command take the value of each cell and do the Insert in the database in the correct fields.

  • Forget the above comment, I set the code here and it worked the Internet thank you very much.

  • Junior Warrior, I’m glad you could work it out, Hug.

0

The name of your column N_CHEKLIST is different where you are setting the parameter name @CHEKLIST. In case it would have to be the same for both.

Answering your question, you would need to treat the code as 'want' save in the bank.

Example:

TBL_CHEKLIST objChek = new TBL_CHEKLIST();

objChek.N_CHEKLIST= (txt_nchklist.Checked == true ? "S" : "N");

In the Insert/Update treat

(!string.IsNullOrEmpty(N_CHEKLIST) ? new SqlParameter("@N_CHEKLIST",txt_nchklist.Text) : new SqlParameter("@N_CHEKLIST", DBNull.Value))};
  • I’m sorry Igor but thank you for your attention, your answer was not very clear to me, that’s because I’m still a novice in c# so I didn’t quite understand what you meant by " you would need to treat the code as 'wish' to save in the bank." the case of the cheklist parameter everything well understood, I just did not understand how to tell my code that when in the column selects the chekbox is marked I write the data of the row in my table and if it is not marked and I do nothing.

  • I think the problem is the following when I populate the datagridview, populate with a table and when I go to record in the bank I no longer see this table, I write the information in another table in the database.

  • Please someone has any more idea how I can do this in my code, the idea of Igor above I could not make it work in my code, if anyone else has another idea thank you.

Browser other questions tagged

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