123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 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 Postavshiki : Form
- {
-
- string id_supplier;
- string name_organization;
- string surname;
- string name;
- string patronymic;
- string floor;
- string phone;
- bool save;
- DataBase dataBase = new DataBase();
- public Postavshiki()
- {
- InitializeComponent();
-
- }
- public void global_FormClosed(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void CreateColumns()
- {
- dataGridView1.Columns.Add("name_organization", "Название организации");
- dataGridView1.Columns.Add("surname", "Фамилия");
- dataGridView1.Columns.Add("name", "Имя");
- dataGridView1.Columns.Add("patronymic", "Отчество");
- dataGridView1.Columns.Add("pol", "Пол");
- dataGridView1.Columns.Add("phone", "Мобильный номер");
- 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), RowState.ModifiedNew);
- }
- private void RefresDataGird(DataGridView dgw)
- {
- dgw.Rows.Clear();
- dataGridView1.Columns[6].Visible = false;
- string queryString = $"Select S.name_organization, S.surname, S.name, S.patronymic, P.pol, S.phone from Supplier S inner join Pol P ON S.id_pol = P.id_pol";
- SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
- dataBase.openConnection();
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- ReadSingleRow(dgw, reader);
- }
- reader.Close();
- }
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- var selectedRow = e.RowIndex;
- if (e.RowIndex >= 0)
- {
- DataGridViewRow row = dataGridView1.Rows[selectedRow];
- name_organization = row.Cells[0].Value.ToString();
- surname = row.Cells[1].Value.ToString();
- name = row.Cells[2].Value.ToString();
- patronymic = row.Cells[3].Value.ToString();
- floor = row.Cells[4].Value.ToString();
- phone = row.Cells[5].Value.ToString();
- dataBase.openConnection();
- SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_supplier From Supplier WHERE name_organization = '{name_organization}'", dataBase.GetConnection());
- id_supplier = 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();
- Postavshiki_add postavshiki_Add = new Postavshiki_add();
- postavshiki_Add.ShowDialog();
- }
- private void button_insert_Click(object sender, EventArgs e)
- {
- if (id_supplier == null)
- {
- MessageBox.Show("Не выделена строчка для изменения");
- }
- else
- {
- this.Hide();
- Postavshiki_update postavshiki_Update = new Postavshiki_update(id_supplier, name_organization, surname, name, patronymic, floor, phone);
- postavshiki_Update.ShowDialog();
- }
- }
- private void Search(DataGridView dgw)
- {
- dgw.Rows.Clear();
- string searchString = $"Select S.name_organization, S.surname, S.name, S.patronymic, P.pol, S.phone from Supplier S inner join Pol P ON S.id_pol = P.id_pol Where name_organization 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[6].Value = RowState.Deleted;
- return;
- }
- dataGridView1.Rows[index].Cells[6].Value = RowState.Deleted;
- id_supplier = null;
- }
- new private void Update()
- {
- dataBase.openConnection();
- for (int index = 0; index < dataGridView1.Rows.Count; index++)
- {
- var rowState = (RowState)dataGridView1.Rows[index].Cells[6].Value;
- if (rowState == RowState.Existed)
- continue;
- if (rowState == RowState.Deleted)
- {
- SqlCommand sqlCommand_supplier = new SqlCommand($"SELECT id_supplier From Supplier WHERE name_organization = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
- var id = sqlCommand_supplier.ExecuteScalar().ToString();
- var deleteQuery = $"delete from Supplier where id_supplier = {id}";
- var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
- command.ExecuteNonQuery();
- }
- }
- dataBase.closeConnection();
- }
- private void button_back_Click(object sender, EventArgs e)
- {
- this.Hide();
- MainMenu main = new MainMenu();
- main.ShowDialog();
- }
- private void button_delete_Click_1(object sender, EventArgs e)
- {
- dataBase.openConnection();
- string admin = $"SELECT P.id_product From Supplier S inner join Products P ON S.id_supplier = P.id_supplier WHERE S.name_organization = '{name_organization}'";
- SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection());
- DataTable dtbl = new DataTable();
- sda.Fill(dtbl);
- if (dtbl.Rows.Count < 1)
- {
- if (id_supplier == null)
- {
- MessageBox.Show("Не выделена запись для удаления!!!");
- }
- else
- {
- deleteRow();
- save = true;
- }
- }
- else
- {
- MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!");
- }
- dataBase.closeConnection();
- }
- private void Postavshiki_Load(object sender, EventArgs e)
- {
- CreateColumns();
- RefresDataGird(dataGridView1);
- }
- private void StrokaSearch_TextChanged_1(object sender, EventArgs e)
- {
- Search(dataGridView1);
- ToolTip t = new ToolTip();
- t.SetToolTip(StrokaSearch, "Поиск осуществляется по названию организации");
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (save == true)
- {
- Update();
- save = false;
- }
- }
- }
- }
|