private void dtDisplay_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dtDisplay.CurrentRow != null) { using (SqlConnection sqlCon = new SqlConnection(connectionString)) { //explicação do procedimento "AddOrEditUsuario" está na consulta "TccCSharp" int randomnumber = random.Next(); int id = MetodosUtilitarios.UltimoUsuario() + 1; sqlCon.Open(); DataGridViewRow dgvRow = dtDisplay.CurrentRow; SqlCommand cmd = new SqlCommand("AddOrEditUsuario", sqlCon); cmd.CommandType = CommandType.StoredProcedure; if (dgvRow.Cells["txtIDdgv"].Value == DBNull.Value) { cmd.Parameters.AddWithValue("@id_usu", 0); } else { cmd.Parameters.AddWithValue("@id_usu", Convert.ToInt32(dgvRow.Cells["txtIDdgv"].Value.ToString().Replace(" ", ""))); } //dgvRow.Cells["txtIDdgv"].Value == DBNull.Value ? "0" : dgvRow.Cells["txtIDdgv"].Value.ToString() cmd.Parameters.AddWithValue("@nome_usu", dgvRow.Cells["txtNomedgv"].Value == DBNull.Value ? "" : dgvRow.Cells["txtNomedgv"].Value.ToString()); cmd.Parameters.AddWithValue("@senha", dgvRow.Cells["txtSenhadgv"].Value == DBNull.Value ? randomnumber.ToString() : dgvRow.Cells["txtSenhadgv"].Value.ToString()); cmd.Parameters.AddWithValue("@dtNasc", dgvRow.Cells["txtDtNasc"].Value == DBNull.Value ? "" : dgvRow.Cells["txtDtNasc"].Value.ToString()); cmd.Parameters.AddWithValue("@endereco", dgvRow.Cells["txtEndereco"].Value == DBNull.Value ? "" : dgvRow.Cells["txtEndereco"].Value.ToString()); cmd.Parameters.AddWithValue("@numero", Convert.ToInt32((dgvRow.Cells["txtNumero"].Value == DBNull.Value ? "0" : dgvRow.Cells["txtNumero"].Value.ToString()).Replace(" ", ""))); cmd.Parameters.AddWithValue("@complemento", dgvRow.Cells["txtComplemento"].Value == DBNull.Value ? "" : dgvRow.Cells["txtComplemento"].Value.ToString()); cmd.Parameters.AddWithValue("@cpf", dgvRow.Cells["txtCpf"].Value == DBNull.Value ? "" : dgvRow.Cells["txtCpf"].Value.ToString()); cmd.Parameters.AddWithValue("@telefone", dgvRow.Cells["txtTelefone"].Value == DBNull.Value ? "" : dgvRow.Cells["txtTelefone"].Value.ToString()); cmd.Parameters.AddWithValue("@email", dgvRow.Cells["txtEmail"].Value == DBNull.Value ? "" : dgvRow.Cells["txtEmail"].Value.ToString()); //https://www.youtube.com/watch?v=cQQy_IfFddg cmd.ExecuteNonQuery(); ShowData(); } } }
//Altera ou adiciona valor valores no DataGridView private void dtDisplay_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dtDisplay.CurrentRow != null) { using (SqlConnection sqlCon = new SqlConnection(connectionString)) { int randomnumber = random.Next(); int id = MetodosUtilitarios.UltimoUsuario() + 1; sqlCon.Open(); DataGridViewRow dgvRow = dtDisplay.CurrentRow; SqlCommand cmd = new SqlCommand("AddOrEditServico", sqlCon); cmd.CommandType = CommandType.StoredProcedure; if (dgvRow.Cells["txtIDservico"].Value == DBNull.Value) { cmd.Parameters.AddWithValue("@id_servico", 0); } else { cmd.Parameters.AddWithValue("@id_servico", Convert.ToInt32(dgvRow.Cells["txtIDservico"].Value.ToString().Replace(" ", ""))); } //dgvRow.Cells["txtIDdgv"].Value == DBNull.Value ? "0" : dgvRow.Cells["txtIDdgv"].Value.ToString() cmd.Parameters.AddWithValue("@id_prestservF", Convert.ToInt32(dgvRow.Cells["txtIDprestserv"].Value == DBNull.Value ? "" : dgvRow.Cells["txtIDprestserv"].Value.ToString())); cmd.Parameters.AddWithValue("@nome_prestservF", dgvRow.Cells["txtNomeprestserv"].Value == DBNull.Value ? "" : dgvRow.Cells["txtNomeprestserv"].Value.ToString()); cmd.Parameters.AddWithValue("@id_usuF", Convert.ToInt32(dgvRow.Cells["txtIDusuario"].Value == DBNull.Value ? "1" : dgvRow.Cells["txtIDusuario"].Value.ToString())); cmd.Parameters.AddWithValue("@nome_usuF", dgvRow.Cells["txtNomeusuario"].Value == DBNull.Value ? "" : dgvRow.Cells["txtNomeusuario"].Value.ToString()); cmd.Parameters.AddWithValue("@avaliacao", Convert.ToInt32(dgvRow.Cells["txtAvaliacao"].Value == DBNull.Value ? "0" : dgvRow.Cells["txtAvaliacao"].Value.ToString())); //https://www.youtube.com/watch?v=cQQy_IfFddg cmd.ExecuteNonQuery(); ShowData(); } } }
private void dtDisplay_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dtDisplay.CurrentRow != null) { using (SqlConnection sqlCon = new SqlConnection(connectionString)) { //explicação do procedimento "AddOrEditAdm" está na consulta "TccCSharp" int randomnumber = random.Next(); int id = MetodosUtilitarios.UltimoUsuario() + 1; sqlCon.Open(); DataGridViewRow dgvRow = dtDisplay.CurrentRow; SqlCommand cmd = new SqlCommand("AddOrEditAdm", sqlCon); cmd.CommandType = CommandType.StoredProcedure; if (dgvRow.Cells["txtIDdgv"].Value == DBNull.Value) { cmd.Parameters.AddWithValue("@id_adm", 0); } else { cmd.Parameters.AddWithValue("@id_adm", Convert.ToInt32(dgvRow.Cells["txtIDdgv"].Value.ToString().Replace(" ", ""))); } //dgvRow.Cells["txtIDdgv"].Value == DBNull.Value ? "0" : dgvRow.Cells["txtIDdgv"].Value.ToString() cmd.Parameters.AddWithValue("@nome_adm", dgvRow.Cells["txtNomedgv"].Value == DBNull.Value ? "" : dgvRow.Cells["txtNomedgv"].Value.ToString()); cmd.Parameters.AddWithValue("@senha", dgvRow.Cells["txtSenhadgv"].Value == DBNull.Value ? randomnumber.ToString() : dgvRow.Cells["txtSenhadgv"].Value.ToString()); if (dgvRow.Cells["txtLoginAdm"].Value.ToString() != "") { if (MetodosUtilitarios.VerificLoginExistenceByLogin(dgvRow.Cells["txtLoginAdm"].Value.ToString()) == "0") { cmd.Parameters.AddWithValue("@login_adm", dgvRow.Cells["txtLoginAdm"].Value.ToString()); } else { SqlConnection sqlConn = new SqlConnection(connectionString); string query = "select login_adm from ADM where id_adm='" + dgvRow.Cells["txtIDdgv"].Value.ToString() + "'"; sqlConn.Open(); SqlCommand comando = new SqlCommand(query, sqlConn); SqlDataReader dr = comando.ExecuteReader(); while (dr.Read()) { output = dr["login_adm"].ToString(); } if (output == dgvRow.Cells["txtLoginAdm"].Value.ToString()) { cmd.Parameters.AddWithValue("@login_adm", dgvRow.Cells["txtLoginAdm"].Value.ToString()); } else { DialogResult loginExiste = MessageBox.Show("Este nome de login já está sendo usado", "Erro de cadastro", MessageBoxButtons.OK); cmd.Parameters.AddWithValue("@login_adm", ""); } } } else { cmd.Parameters.AddWithValue("@login_adm", ""); } cmd.Parameters.AddWithValue("@email", dgvRow.Cells["txtEmail"].Value == DBNull.Value ? "" : dgvRow.Cells["txtEmail"].Value.ToString()); //https://www.youtube.com/watch?v=cQQy_IfFddg cmd.ExecuteNonQuery(); if (dgvRow.Cells["txtIDdgv"].Value == DBNull.Value) { cmd.Parameters.AddWithValue("@id_adm", 0); dtDisplay.AllowUserToAddRows = false; } ShowData(); } } }