123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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.Data.SqlClient;
- using System.Windows.Forms;
- namespace proba
- {
- public partial class Form2 : Form
- {
- string id_sale;
- string product;
- string date_sale;
- string employee;
- string summa;
- bool save;
- DataBase dataBase = new DataBase();
- public Form2()
- {
- InitializeComponent();
- }
- private void CreateColumns()
- {
- dataGridView1.Columns.Add("product", "Название товара");
- dataGridView1.Columns.Add("date_sale", "Дата продажи");
- dataGridView1.Columns.Add("employee", "Сотрудник");
- dataGridView1.Columns.Add("summa", "Сумма продажи");
- 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), RowState.ModifiedNew);
- }
- private void RefresDataGird(DataGridView dgw)
- {
- dgw.Rows.Clear();
- dataGridView1.Columns[4].Visible = false;
- string queryString = $"Select P.product, S.date_sale, E.surname, S.summa from Sales S inner join Products P ON P.id_product=S.id_product inner join Employee E ON S.id_employee=E.id_employee";
- SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
- dataBase.openConnection();
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- ReadSingleRow(dgw, reader);
- }
- reader.Close();
- }
- private void button_back_Click(object sender, EventArgs e)
- {
- this.Hide();
- MainMenu mainMenu = new MainMenu();
- mainMenu.ShowDialog();
- }
- public void global_FormClosed(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void pictureBox2_Click(object sender, EventArgs e)
- {
- RefresDataGird(dataGridView1);
- }
- private void Search(DataGridView dgw)
- {
- dgw.Rows.Clear();
- string searchString = $"Select P.product, S.date_sale, E.surname, S.Summa from Sales S inner join Products P ON P.id_product=S.id_product inner join Employee E ON S.id_employee=E.id_employee Where P.product 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 StrokaSearch_TextChanged(object sender, EventArgs e)
- {
- Search(dataGridView1);
- }
- private void Form2_Load(object sender, EventArgs e)
- {
- CreateColumns();
- RefresDataGird(dataGridView1);
- }
- 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[4].Value = RowState.Deleted;
- return;
- }
- dataGridView1.Rows[index].Cells[4].Value = RowState.Deleted;
- id_sale = null;
- }
- private void button_delete_Click(object sender, EventArgs e)
- {
- if (id_sale == null)
- {
- MessageBox.Show("Не выделена запись для удаления!!!");
- }
- else
- {
- deleteRow();
- save = true;
- }
- }
- new private void Update()
- {
- dataBase.openConnection();
- for (int index = 0; index < dataGridView1.Rows.Count; index++)
- {
- var rowState = (RowState)dataGridView1.Rows[index].Cells[4].Value;
- if (rowState == RowState.Existed)
- continue;
- if (rowState == RowState.Deleted)
- {
- SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_sale From Sales S inner join Products P ON S.id_product=P.id_product WHERE P.product = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
- var id = sqlCommand_product.ExecuteScalar().ToString();
- var deleteQuery = $"delete from Sales where id_sale = {id}";
- var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
- command.ExecuteNonQuery();
- }
- }
- 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_sale == null)
- {
- MessageBox.Show("Не выделена строчка для изменения");
- }
- else
- {
- this.Hide();
- Prodaja_update prodaja_Update = new Prodaja_update(id_sale, product, date_sale, employee, summa);
- prodaja_Update.ShowDialog();
- }
- }
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- var selectedRow = e.RowIndex;
- if (e.RowIndex >= 0)
- {
- DataGridViewRow row = dataGridView1.Rows[selectedRow];
- product = row.Cells[0].Value.ToString();
- date_sale = row.Cells[1].Value.ToString();
- employee = row.Cells[2].Value.ToString();
- summa = row.Cells[3].Value.ToString();
- dataBase.openConnection();
- SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_sale From Sales S inner join Products P ON S.id_product=P.id_product WHERE P.product = '{product}'", dataBase.GetConnection());
- id_sale = sqlCommand_product.ExecuteScalar().ToString();
- dataBase.closeConnection();
- }
- }
- private void pictureBox2_Click_1(object sender, EventArgs e)
- {
- RefresDataGird(dataGridView1);
- save = false;
- }
- private void button_add_Click(object sender, EventArgs e)
- {
- this.Hide();
- Sales sales = new Sales();
- sales.ShowDialog();
- }
- private void button_saleDay_Click(object sender, EventArgs e)
- {
- this.Hide();
- SummaSaleDay summaSaleDay = new SummaSaleDay();
- summaSaleDay.ShowDialog();
- }
- }
- }
|