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 project { enum RowState { Existed, New, ModifiedNew, Deleted } public partial class Main : Form { string code, name_dan, adres_dan, city_dan, number_dan, contact_dan; public static string login; DataBase dataBase = new DataBase(); public Main() { InitializeComponent(); } private void ReadSingleRow(DataGridView dgw, IDataRecord record) { dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5)); } private void RefresDataGird(DataGridView dgw) { dgw.Rows.Clear(); string queryString = $"Select * from Dannye"; SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { ReadSingleRow(dgw, reader); } reader.Close(); } private void CreateColumns() { dataGridView1.Columns.Add("code", "ID"); dataGridView1.Columns.Add("name_dan", "Наименование"); dataGridView1.Columns.Add("adres_dan", "Адрес"); dataGridView1.Columns.Add("city_dan", "Город"); dataGridView1.Columns.Add("number_dan", "Телефон"); dataGridView1.Columns.Add("contact_dan", "Контактная информация"); } private void Main_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); } private void Main_Load(object sender, EventArgs e) { CreateColumns(); RefresDataGird(dataGridView1); string proverkaLogin = Login.login; dataBase.openConnection(); // Проверка является ли пользователь администратором, если да – открывается доступ к 2 двум кнопка работы над таблицей справочника SqlCommand sqlCommand2 = new SqlCommand($"SELECT administrator From Employee WHERE login = '{proverkaLogin}'", dataBase.GetConnection()); string admPolzovatel = sqlCommand2.ExecuteScalar().ToString(); if (admPolzovatel.Trim() == "true") { button2.Visible = true; button4.Visible = true; } } private void pictureBox1_Click(object sender, EventArgs e) { Profile profile = new Profile(); profile.Show(); } private void button2_Click(object sender, EventArgs e) { deleteRow(); Update(); } 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[5].Value = RowState.Deleted; return; } dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted; } new private void Update() { dataBase.openConnection(); for (int index = 0; index < dataGridView1.Rows.Count; index++) { try { var rowState = (RowState)dataGridView1.Rows[index].Cells[5].Value; if (rowState == RowState.Existed) continue; if (rowState == RowState.Deleted) { var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value); var deleteQuery = $"delete from Dannye where code = {id}"; var command = new SqlCommand(deleteQuery, dataBase.GetConnection()); command.ExecuteNonQuery(); } } catch { } } dataBase.closeConnection(); } private void button4_Click(object sender, EventArgs e) { this.Hide(); DannyeAdd add = new DannyeAdd(); add.Show(); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { var selectedRow = e.RowIndex; if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[selectedRow]; code = row.Cells[0].Value.ToString(); name_dan = row.Cells[1].Value.ToString(); adres_dan = row.Cells[2].Value.ToString(); city_dan = row.Cells[3].Value.ToString(); number_dan = row.Cells[4].Value.ToString(); contact_dan = row.Cells[5].Value.ToString(); } } } }