using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace proba { public partial class Employee : Form { string id_employee; string surname; string name; string patronymic; string pol; string date_of_birth; string phone; string login; bool save; DataBase dataBase = new DataBase(); public Employee() { InitializeComponent(); } private void CreateColumns() { dataGridView1.Columns.Add("surname", "Фамилия"); dataGridView1.Columns.Add("name", "Имя"); dataGridView1.Columns.Add("patronymic", "Отчество"); dataGridView1.Columns.Add("pol", "Пол"); dataGridView1.Columns.Add("date_of_birth", "Дата рождения"); dataGridView1.Columns.Add("phone", "Телефон"); dataGridView1.Columns.Add("login", "Логин"); dataGridView1.Columns.Add("", String.Empty); } private void ReadSingleRow(DataGridView dgw, IDataRecord record) { dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6), RowState.ModifiedNew); } private void RefresDataGird(DataGridView dgw) { dgw.Rows.Clear(); dataGridView1.Columns[7].Visible = false; string queryString = $"Select E.surname, E.name, E.patronymic, P.pol, E.date_of_birth, E.phone, E.login From Employee E inner join Pol P ON E.id_pol = P.id_pol inner join Roles R ON E.id_role = R.id_role WHERE Not R.role = 'Администратор'"; SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { ReadSingleRow(dgw, reader); } reader.Close(); } public void global_FormClosed(object sender, EventArgs e) { Application.Exit(); } private void Search(DataGridView dgw) { dgw.Rows.Clear(); string searchString = $"Select E.surname, E.name, E.patronymic, P.pol, E.date_of_birth, E.phone, E.login From Employee E inner join Pol P ON E.id_pol = P.id_pol inner join Roles R ON E.id_role = R.id_role WHERE Not R.role = 'Администратор' and E.login like '%" + StrokaSearch.Text + "%'"; SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader read = com.ExecuteReader(); while (read.Read()) { ReadSingleRow(dgw, read); } read.Close(); } private void deleteRow() { int index = dataGridView1.CurrentCell.RowIndex; dataGridView1.Rows[index].Visible = false; if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty) { dataGridView1.Rows[index].Cells[7].Value = RowState.Deleted; return; } dataGridView1.Rows[index].Cells[7].Value = RowState.Deleted; id_employee = null; } new private void Update() { dataBase.openConnection(); for (int index = 0; index < dataGridView1.Rows.Count; index++) { var rowState = (RowState)dataGridView1.Rows[index].Cells[7].Value; if (rowState == RowState.Existed) continue; if (rowState == RowState.Deleted) { SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_employee From Employee WHERE surname = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection()); var id = sqlCommand_product.ExecuteScalar().ToString(); var deleteQuery = $"delete from Employee where id_employee = {id}"; var command = new SqlCommand(deleteQuery, dataBase.GetConnection()); command.ExecuteNonQuery(); } } dataBase.closeConnection(); } private void Employee_Load(object sender, EventArgs e) { CreateColumns(); RefresDataGird(dataGridView1); } private void button_back_Click(object sender, EventArgs e) { this.Hide(); MainMenu mainMenu_Admin = new MainMenu(); mainMenu_Admin.ShowDialog(); } private void StrokaSearch_TextChanged(object sender, EventArgs e) { Search(dataGridView1); } private void button_delete_Click(object sender, EventArgs e) { dataBase.openConnection(); string admin = $"SELECT S.id_sale From Employee E inner join Sales S ON E.id_employee = S.id_employee WHERE E.surname = '{surname}'"; SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection()); DataTable dtbl = new DataTable(); sda.Fill(dtbl); if (dtbl.Rows.Count < 1) { if (id_employee == null) { MessageBox.Show("Не выделена запись для удаления!!!"); } else { deleteRow(); save = true; } } else { MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!"); } dataBase.closeConnection(); } private void buttonSave_Click(object sender, EventArgs e) { if (save == true) { Update(); save = false; } } private void button_insert_Click(object sender, EventArgs e) { if (id_employee == null) { MessageBox.Show("Не выделена строчка для изменения"); } else { this.Hide(); EmployeeUpdate employeeUpdate = new EmployeeUpdate(id_employee, surname, name, patronymic, pol, date_of_birth, phone, login); employeeUpdate.ShowDialog(); } } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { var selectedRow = e.RowIndex; if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[selectedRow]; dataBase.openConnection(); surname = row.Cells[0].Value.ToString(); name = row.Cells[1].Value.ToString(); patronymic = row.Cells[2].Value.ToString(); pol = row.Cells[3].Value.ToString(); date_of_birth = row.Cells[4].Value.ToString(); phone = row.Cells[5].Value.ToString(); login = row.Cells[6].Value.ToString(); SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_employee From Employee WHERE surname = '{surname}'", dataBase.GetConnection()); id_employee = sqlCommand_product.ExecuteScalar().ToString(); dataBase.closeConnection(); } } private void pictureBox2_Click(object sender, EventArgs e) { RefresDataGird(dataGridView1); save = false; } private void button_add_Click(object sender, EventArgs e) { this.Hide(); EmployeeAdd employeeAdd = new EmployeeAdd(); employeeAdd.ShowDialog(); } } }