123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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 Издатель : Form
- {
- string id_edition;
- string edition;
- bool save;
- DataBase dataBase = new DataBase();
- public Издатель()
- {
- InitializeComponent();
- }
- private void CreateColumns()
- {
- EditionTab.Columns.Add("edition", "Название издательства");
- EditionTab.Columns.Add("", String.Empty);
- }
- private void ReadSingleRow(DataGridView dgw, IDataRecord record)
- {
- dgw.Rows.Add(record.GetString(0), RowState.ModifiedNew);
- }
- private void RefresDataGird(DataGridView dgw)
- {
- dgw.Rows.Clear();
- EditionTab.Columns[1].Visible = false;
- string queryString = $"Select edition from Edition";
- SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
- dataBase.openConnection();
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- ReadSingleRow(dgw, reader);
- }
- reader.Close();
- }
- private void button_insert_Click(object sender, EventArgs e)
- {
- if (id_edition == null)
- {
- MessageBox.Show("Не выделена строчка для изменения");
- }
- else
- {
- this.Hide();
- EditionUpd Upd = new EditionUpd(id_edition, edition);
- Upd.ShowDialog();
- }
- }
- private void button_add_Click(object sender, EventArgs e)
- {
- this.Hide();
- EditionAdd edition_Add = new EditionAdd();
- edition_Add.ShowDialog();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (save == true)
- {
- Update();
- save = false;
- }
- }
- public void global_FormClosed(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void deleteRow()
- {
- int index = EditionTab.CurrentCell.RowIndex;
- EditionTab.Rows[index].Visible = false;
- if (EditionTab.Rows[index].Cells[0].Value.ToString() == String.Empty)
- {
- EditionTab.Rows[index].Cells[1].Value = RowState.Deleted;
- return;
- }
- EditionTab.Rows[index].Cells[1].Value = RowState.Deleted;
- id_edition = null;
- }
- new private void Update()
- {
- dataBase.openConnection();
- for (int index = 0; index < EditionTab.Rows.Count; index++)
- {
- var rowState = (RowState)EditionTab.Rows[index].Cells[1].Value;
- if (rowState == RowState.Existed)
- continue;
- if (rowState == RowState.Deleted)
- {
- SqlCommand sqlCommand_edition = new SqlCommand($"SELECT id_edition From Edition WHERE edition = '{EditionTab.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
- var id = sqlCommand_edition.ExecuteScalar().ToString();
- var deleteQuery = $"delete from Edition where id_edition = {id}";
- var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
- command.ExecuteNonQuery();
- }
- }
- dataBase.closeConnection();
- }
- private void StrokaSearch_TextChanged(object sender, EventArgs e)
- {
- Search(EditionTab);
- }
- private void Search(DataGridView dgw)
- {
- dgw.Rows.Clear();
- string searchString = $"Select edition from Edition Where edition 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 dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
- {
- var selectedRow = e.RowIndex;
- if (e.RowIndex >= 0)
- {
- DataGridViewRow row = EditionTab.Rows[selectedRow];
- edition = row.Cells[0].Value.ToString();
- SqlCommand sqlCommand_edition = new SqlCommand($"SELECT id_edition From Edition WHERE edition = '{edition}'", dataBase.GetConnection());
- id_edition = sqlCommand_edition.ExecuteScalar().ToString();
- }
- }
- private void StrokaSearch_TextChanged_1(object sender, EventArgs e)
- {
- Search(EditionTab);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- CreateColumns();
- RefresDataGird(EditionTab);
- }
- private void button_delete_Click(object sender, EventArgs e)
- {
- dataBase.openConnection();
- string admin = $"SELECT P.id_product From Edition E inner join Products P ON E.id_edition = P.id_edition WHERE E.edition = '{edition}'";
- SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection());
- DataTable dtbl = new DataTable();
- sda.Fill(dtbl);
- if (dtbl.Rows.Count < 1)
- {
- if (id_edition == null)
- {
- MessageBox.Show("Не выделена запись для удаления!!!");
- }
- else
- {
- deleteRow();
- save = true;
- }
- }
- else
- {
- MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!");
- }
- dataBase.closeConnection();
- }
- private void pictureBox1_Click_1(object sender, EventArgs e)
- {
- RefresDataGird(EditionTab);
- save = false;
- }
- private void button_back_Click(object sender, EventArgs e)
- {
- this.Hide();
- MainMenu mainMenu = new MainMenu();
- mainMenu.ShowDialog();
- }
- }
- }
|