//mostrar valores no formulario private void ShowValues(Library.Cargo cargo) { this.codigoTB.Text = string.Format("{0}", cargo.Id); this.nomeTB.Text = cargo.Nome; this.descricaoTB.Text = cargo.Descricao; //cargo.DataCadastro; }
private void excluirButton_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; this.InputDisabler(false); /************BEGIN************/ //achando idCargo selecionado na grid Library.Cargo cargo = null; for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) { cargo = Library.CargoBD.FindById((long)this.dataGridView1.SelectedRows[i].Cells["idDGVTBC"].Value); } if (MessageBox.Show(this.fcgCargoDeletarMsg, this.fcgCargoDeletarMsgTitle, MessageBoxButtons.YesNo) == DialogResult.Yes) { //se não tiver selecionado mostra mensagem se estiver deleta e atualiza formulario if (dataGridView1.SelectedRows.Count != 1) { MessageBox.Show(this.fcgCargoDeletarNoSelected); } else { Library.CargoBD.Delete(cargo); this.RefreshForm(); } } /*************END*************/ this.Modo = "Excluir"; this.Cursor = Cursors.Default; }
static public void Update(Library.Cargo cargo) { SqlConnection conexao = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); SqlCommand comando = conexao.CreateCommand(); comando.CommandText = "UPDATE Cargo SET nome = @nome, descricao = @descricao, dataCadastro = @dataCadastro WHERE (id= " + cargo.Id + ")"; comando.Parameters.AddWithValue("@nome", cargo.Nome); comando.Parameters.AddWithValue("@descricao", cargo.Descricao); comando.Parameters.AddWithValue("@dataCadastro", cargo.DataCadastro); conexao.Open(); comando.ExecuteNonQuery(); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex);; } finally { conexao.Close(); } }
static public void Save(Library.Cargo cargo) { SqlConnection conexao = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); SqlCommand comando = conexao.CreateCommand(); comando.CommandText = "INSERT INTO Cargo (nome, descricao, dataCadastro) VALUES (@nome, @descricao, @dataCadastro)" + "SELECT CAST(scope_identity() AS bigint)"; comando.Parameters.AddWithValue("@nome", cargo.Nome); comando.Parameters.AddWithValue("@descricao", cargo.Descricao); comando.Parameters.AddWithValue("@dataCadastro", cargo.DataCadastro); conexao.Open(); //comando.ExecuteNonQuery(); cargo.Id = (long)comando.ExecuteScalar(); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex);; } finally { conexao.Close(); } }
//atualizar objeto cargo com valores do formulario private Library.Cargo UpdateCargoFromForm() { Library.Cargo cargoTemp = this.cargo; //cargoTemp.Id = ; cargoTemp.Nome = this.nomeTB.Text; cargoTemp.Descricao = this.descricaoTB.Text; //cargoTemp.DataCadastro = DateTime.Now; return(cargoTemp); }
private void salvarButton_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; /************BEGIN************/ if (this.ValidateForm()) { this.MenuDisabler(true, false, false, false, false); this.InputDisabler(false); if (this.modo == "Cadastrar") { //criando um cargo com valores do formulário this.cargo = this.ReturnCargoFromForm(); //salvando cargo Library.CargoBD.Save(this.cargo); this.dataGridView1.Rows.Add(this.cargo.Id, this.cargo.Nome, this.cargo.DataCadastro); } else { this.cargo = this.UpdateCargoFromForm(); //atualizando cargo Library.CargoBD.Update(this.cargo); foreach (DataGridViewRow d in dataGridView1.Rows) { if ((long)d.Cells[0].Value == this.cargo.Id) { d.Cells[0].Value = this.cargo.Id; d.Cells[1].Value = this.cargo.Nome; d.Cells[2].Value = this.cargo.DataCadastro; } } } //atualizando formulário //RefreshForm(); RefreshChilds(); this.Modo = "Salvar"; } else { //mensagem de erro } /*************END*************/ this.Cursor = Cursors.Default; }
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { this.Cursor = Cursors.WaitCursor; this.MenuDisabler(true, true, false, false, true); this.InputDisabler(false); this.Modo = "Selecionar"; /************BEGIN************/ if (dataGridView1.SelectedRows.Count == 1) { //achando idCargo selecionado na grid long idCargo = (long)this.dataGridView1.SelectedRows[0].Cells["idDGVTBC"].Value; //mostrando cargo selecionado no formulario this.cargo = Library.CargoBD.FindById(idCargo); this.ShowValues(cargo); } /*************END*************/ this.Cursor = Cursors.Default; }
private void editarButton_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; this.MenuDisabler(true, false, true, true, false); this.InputDisabler(true); /************BEGIN************/ //achando idCargo selecionado na grid long idCargo = 0; for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) { idCargo = (long)this.dataGridView1.SelectedRows[i].Cells["idDGVTBC"].Value; } //mostrando cargo selecionado no formulario this.cargo = Library.CargoBD.FindById(idCargo); this.ShowValues(this.cargo); /*************END*************/ this.Modo = "Editar"; this.Cursor = Cursors.Default; }
static public Library.Cargo FindById(long idCargo) { SqlConnection conexao = null; SqlDataAdapter dap = null; DataSet ds = null; Library.Cargo cargo = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); dap = new SqlDataAdapter("SELECT * FROM Cargo WHERE id='" + idCargo + "'", conexao); ds = new DataSet(); dap.Fill(ds, "Cargo"); if (ds.Tables["Cargo"].Rows.Count == 1) { cargo = new Cargo(); cargo.Id = (long)ds.Tables["Cargo"].Rows[0]["id"]; cargo.Nome = ds.Tables["Cargo"].Rows[0]["nome"].ToString(); cargo.Descricao = ds.Tables["Cargo"].Rows[0]["descricao"].ToString(); cargo.DataCadastro = (DateTime)ds.Tables["Cargo"].Rows[0]["dataCadastro"]; } return(cargo); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex);; } finally { conexao.Close(); dap.Dispose(); ds.Dispose(); } return(null); }
static public void Delete(Library.Cargo cargo) { SqlConnection conexao = null; try { conexao = new SqlConnection(global::Connection.Connection.String()); SqlCommand comando = conexao.CreateCommand(); comando.CommandText = "DELETE FROM Cargo WHERE id='" + cargo.Id + "'"; conexao.Open(); comando.ExecuteNonQuery(); } catch (Exception ex) { Library.Diagnostics.Logger.Error(ex); } finally { conexao.Close(); } }