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 Sales : Form { string id_product; string product; string description; string price; string year_publishing; string country; string edition; string genre; string performer; string supplier; public static int countBuyProducts = 0; public static double summaBuyProducts = 0; DataBase dataBase = new DataBase(); public Sales() { InitializeComponent(); } private void CreateColumns() { dataGridView1.Columns.Add("product", "Номенклатура"); dataGridView1.Columns.Add("description", "Описание"); dataGridView1.Columns.Add("price", "Цена"); dataGridView1.Columns.Add("year_publishing", "Год издания"); dataGridView1.Columns.Add("country", "Страна производства"); dataGridView1.Columns.Add("edition", "Издательство"); dataGridView1.Columns.Add("genre", "Жанр музыки"); dataGridView1.Columns.Add("performer", "Исполнитель"); dataGridView1.Columns.Add("supplier", "Поставщик"); } private void ReadSingleRow(DataGridView dgw, IDataRecord record) { dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetDouble(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6), record.GetString(7), record.GetString(8)); } private void RefresDataGird(DataGridView dgw) { dgw.Rows.Clear(); string queryString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL"; SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { ReadSingleRow(dgw, reader); } reader.Close(); } private void Sales_Load(object sender, EventArgs e) { CreateColumns(); RefresDataGird(dataGridView1); } private void button_back_Click_1(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Продано товаров: " + countBuyProducts +"\nСовершена продажа на сумму: " + summaBuyProducts, "Информация", MessageBoxButtons.OK); if (dialogResult == DialogResult.OK) { this.Hide(); MainMenu mainMenu = new MainMenu(); mainMenu.ShowDialog(); } } public void global_FormClosed(object sender, EventArgs e) { Application.Exit(); } private void button_buy_Click(object sender, EventArgs e) { if (id_product == null) { MessageBox.Show("Не выделена номенклатура для покупки"); } else { this.Hide(); BuyTools buyTools = new BuyTools(id_product, product, description, price, year_publishing, country, edition, genre, performer, supplier); buyTools.ShowDialog(); } } private void pictureBox2_Click(object sender, EventArgs e) { RefresDataGird(dataGridView1); } private void text_id_view_tools_SelectedIndexChanged(object sender, EventArgs e) { Search(dataGridView1); } private void Search(DataGridView dgw) { dgw.Rows.Clear(); string searchString = ""; if (text_name.Text.Replace(" ", "") != "") { searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and product like '%" + text_name.Text + "%'"; } else if (text_price1.Text.Replace(" ", "") != "") { searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and price > '" + text_price1.Text + "'"; } else if (text_price2.Text.Replace(" ", "") != "") { searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and price < '" + text_price2.Text + "'"; } else { searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL"; } SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection()); dataBase.openConnection(); SqlDataReader read = com.ExecuteReader(); while (read.Read()) { ReadSingleRow(dgw, read); } read.Close(); } private void text_name_TextChanged(object sender, EventArgs e) { Search(dataGridView1); } private void text_price1_TextChanged(object sender, EventArgs e) { Search(dataGridView1); } private void text_price2_TextChanged(object sender, EventArgs e) { Search(dataGridView1); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { var selectedRow = e.RowIndex; if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[selectedRow]; dataBase.openConnection(); product = row.Cells[0].Value.ToString(); description = row.Cells[1].Value.ToString(); price = row.Cells[2].Value.ToString(); year_publishing = row.Cells[3].Value.ToString(); country = row.Cells[4].Value.ToString(); edition = row.Cells[5].Value.ToString(); genre = row.Cells[6].Value.ToString(); performer = row.Cells[7].Value.ToString(); supplier = row.Cells[8].Value.ToString(); SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_product From Products WHERE product = '{product}'", dataBase.GetConnection()); id_product = sqlCommand_product.ExecuteScalar().ToString(); dataBase.closeConnection(); } } } }