Select row in datagridwier and write to database

0

I have a datagridviwer populated by a sql database query, in that datagrid I have a column with a chekbox, now comes my doubt, how do I write to the database when the row has been selected by the user in the chekbox.

Follow my datagrid screen

codetodaywhereIinsertitintothebankbutwithoutrunningthechekbox

privatevoidncheklist(){SqlCommandcmd=newSqlCommand();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();
}
    
asked by anonymous 26.09.2017 / 15:20

2 answers

0

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

// 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
   }
}
    
26.09.2017 / 22:30
0

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

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

Example:

TBL_CHEKLIST objChek = new TBL_CHEKLIST();

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

No Insert/Update do due treatment

(!string.IsNullOrEmpty(N_CHEKLIST) ? new SqlParameter("@N_CHEKLIST",txt_nchklist.Text) : new SqlParameter("@N_CHEKLIST", DBNull.Value))};
    
26.09.2017 / 15:47