private void EditProductInOrder_Button_Click(object sender, RoutedEventArgs e) { try { var index = dataGrid_EditOrder.SelectedIndex; // Проверка на то, что была выделена ячейка if (index > -1) { using (OrdersdbEntities db = new OrdersdbEntities()) { // При выборе строки получение ID заказа object item = dataGrid_EditOrder.SelectedItem; string nameProduct = (dataGrid_EditOrder.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text.ToString(); TextBox_count.Text = (dataGrid_EditOrder.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text.ToString(); //MessageBox.Show(nameProduct); comboBox_nameProduct.SelectedItem = nameProduct; } } } catch (Exception e1) { MessageBox.Show(e1.Message, "EditProductInOrder_Button_Click", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void AddProduct1_Button_Click(object sender, RoutedEventArgs e) { try { // Определение product_id по comboBox_nameProduct int product_id_temp = 0; using (OrdersdbEntities db = new OrdersdbEntities()) { product_id_temp = db.Products.Where(x => x.product_name == comboBox_nameProduct.SelectedValue.ToString()).Select(x => x.product_id).First(); } if (comboBox_nameProduct.SelectedValue.ToString() != "Не выбрано" && Convert.ToInt32(TextBox_count.Text) > 0) { //dataGrid_EditOrder.Items.Add(new Item() { product_id = product_id_temp, product_name = comboBox_nameProduct.SelectedValue.ToString(), item_count = TextBox_count.Text }); col.Add(new Item() { product_id = product_id_temp, product_name = comboBox_nameProduct.SelectedValue.ToString(), item_count = TextBox_count.Text }); dataGrid_EditOrder.ItemsSource = col; } else { MessageBox.Show("Выберите товар или введите количество товаров больше 0", "Предупреждение"); } } catch (Exception e1) { MessageBox.Show(e1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); //MessageBox.Show("Выберите товар или введите количество товаров больше 0", "Предупреждение"); } }
// Загрузка данных из базы dataGrid_Orders public void Load_dataGrid_Orders() { try { witoutSort.Background = Brushes.Gold; withSort.Background = Brushes.LightGray; using (OrdersdbEntities db = new OrdersdbEntities()) { var dataFromQuary = from o in db.Orders.AsEnumerable() from l in db.List_of_order_items from p in db.Products where o.list_of_order_items == l.list_of_order_items__id where l.item_product_id == p.product_id group new { db.Orders, db.List_of_order_items, db.Products, l.item_count, p.product_price } by new { o.orders_id, o.date, o.name_client } into g select new { orders_id = g.Key.orders_id, date = g.Key.date.ToShortDateString(), name_client = g.Key.name_client, totalSumOrder = g.Sum(x => x.item_count * x.product_price) }; dataGrid_Orders.ItemsSource = dataFromQuary; } } catch (Exception e) { MessageBox.Show(e.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void EditOrder_Button_Click(object sender, RoutedEventArgs e) { try { var index = dataGrid_Orders.SelectedIndex; // Проверка на то, что была выделена ячейка if (index > -1) { using (OrdersdbEntities db = new OrdersdbEntities()) { // При выборе строки получение ID заказа object item = dataGrid_Orders.SelectedItem; EditOrder editOrder = new EditOrder(); editOrder.orders_id_temp_EditOrder = Convert.ToInt32((dataGrid_Orders.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text); editOrder.list_of_order_items_temp_EditOrder = db.Orders.Where(x => x.orders_id == editOrder.orders_id_temp_EditOrder) .Select(x => x.list_of_order_items).FirstOrDefault(); editOrder.ShowDialog(); } } } catch (Exception e1) { MessageBox.Show(e1.Message, "EditOrder_Button_Click", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void DeleteOrder_Button_Click(object sender, RoutedEventArgs e) { try { var index = dataGrid_Orders.SelectedIndex; // Проверка на то, что была выделена ячейка if (index > -1) { if (MessageBox.Show("Вы действительно хотите удалить заказ?", "Удаление заказа", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes) { // При выборе строки получение ID заказа object item = dataGrid_Orders.SelectedItem; int orders_id_temp = Convert.ToInt32((dataGrid_Orders.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text); // Определение list_of_order_items int list_of_order_items_temp = 0; using (OrdersdbEntities db = new OrdersdbEntities()) { list_of_order_items_temp = db.Orders.Where(x => x.orders_id == orders_id_temp).Select(x => x.list_of_order_items).DefaultIfEmpty().Single(); Debug.WriteLine(list_of_order_items_temp); //List_of_order_items list_of_order_items = new List_of_order_items() { list_of_order_items__id = list_of_order_items_temp }; //db.List_of_order_items.Attach(list_of_order_items); //db.List_of_order_items.Remove(list_of_order_items); //db.SaveChanges(); db.Database.ExecuteSqlCommand("DELETE FROM [dbo].[List_of_order_items] WHERE [list_of_order_items__id] = {0}", new object[] { list_of_order_items_temp }); } using (OrdersdbEntities db = new OrdersdbEntities()) { Order order = new Order() { orders_id = orders_id_temp }; db.Orders.Attach(order); db.Orders.Remove(order); db.SaveChanges(); } Load_dataGrid_Orders(); dataGrid_List_of_order_items.ItemsSource = null; } } } catch (Exception e2) { MessageBox.Show(e2.Message, "DeleteOrder_Button_Click", MessageBoxButton.OK, MessageBoxImage.Error); } }
// Загрузка начальных значений в окно EditOrder public void LoadDataInEditOrder(object sender, RoutedEventArgs e) { //MessageBox.Show(this.orders_id_temp_EditOrder.ToString()); try { using (OrdersdbEntities db = new OrdersdbEntities()) { TextBox_NameClient.Text = db.Orders.Where(x => x.orders_id == orders_id_temp_EditOrder) .Select(x => x.name_client).First(); comboBox_nameProduct.Items.Add("Не выбрано"); var product = db.Products; foreach (Product u in product) { comboBox_nameProduct.Items.Add(u.product_name); } comboBox_nameProduct.SelectedIndex = 0; TextBox_count.Text = "0"; var dataFromQuary = from o in db.Orders.AsEnumerable() from l in db.List_of_order_items from p in db.Products where o.orders_id == Convert.ToInt32(orders_id_temp_EditOrder) where l.list_of_order_items__id == o.list_of_order_items where l.item_product_id == p.product_id select new { product_id = p.product_id, product_name = p.product_name, item_count = l.item_count, //product_price = p.product_price }; foreach (var u in dataFromQuary) { //dataGrid_EditOrder.Items.Add(new Item() { product_id = u.product_id, product_name = u.product_name, item_count = u.item_count1.ToString() }); col.Add(new Item() { product_id = u.product_id, product_name = u.product_name, item_count = u.item_count.ToString() }); } dataGrid_EditOrder.ItemsSource = col; } } catch (Exception e2) { MessageBox.Show(e2.Message, "LoadDataInEditOrder", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void SaveProductInOrder_Button_Click(object sender, RoutedEventArgs e) { try { using (OrdersdbEntities db = new OrdersdbEntities()) { if (dataGrid_EditOrder.SelectedIndex >= 0) { //MessageBox.Show("SelectedIndex"); // get the selected's infomation first Item item_New = new Item(); //Item item = dataGrid_EditOrder.SelectedItem as Item; //MessageBox.Show(" " + item.product_id + " " + item.product_name + " " + item.item_count); item_New.product_id = db.Products .Where(x => x.product_name == comboBox_nameProduct.SelectedItem.ToString()) .Select(x => x.product_id) .FirstOrDefault(); item_New.product_name = comboBox_nameProduct.SelectedItem.ToString(); item_New.item_count = TextBox_count.Text.Trim(); // remove the old information Item item_Old = dataGrid_EditOrder.SelectedItem as Item; col.Remove(item_Old); //add a new customer col.Add(item_New); // Обновить dataGrid_EditOrder //foreach (var u in col) //{ // col.Add(new Item() { product_id = u.product_id, product_name = u.product_name, item_count = u.item_count.ToString() }); //} dataGrid_EditOrder.ItemsSource = col; } //else { MessageBox.Show("Выделите строку"); } } } catch (Exception e1) { MessageBox.Show(e1.Message, "SaveProductInOrder_Button_Click", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Sorting_Button_Click(object sender, RoutedEventArgs e) { withSort.Background = Brushes.Gold; witoutSort.Background = Brushes.LightGray; try { DateTime fromDay = (new DateTime( Convert.ToInt32(from_year_TextBox.Text), Convert.ToInt32(from_month_TextBox.Text), Convert.ToInt32(from_day_TextBox.Text), 0, 0, 0)); DateTime toDay = (new DateTime( Convert.ToInt32(to_year_TextBox.Text), Convert.ToInt32(to_month_TextBox.Text), Convert.ToInt32(to_day_TextBox.Text), 23, 59, 59)); using (OrdersdbEntities db = new OrdersdbEntities()) { var dataFromQuary = from o in db.Orders.AsEnumerable() from l in db.List_of_order_items from p in db.Products where o.list_of_order_items == l.list_of_order_items__id where l.item_product_id == p.product_id where o.date >= fromDay && o.date <= toDay group new { db.Orders, db.List_of_order_items, db.Products, l.item_count, p.product_price } by new { o.orders_id, o.date, o.name_client } into g select new { orders_id = g.Key.orders_id, date = g.Key.date.ToShortDateString(), name_client = g.Key.name_client, totalSumOrder = g.Sum(x => x.item_count * x.product_price) }; dataGrid_Orders.ItemsSource = dataFromQuary; } } catch (Exception e1) { MessageBox.Show(e1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } }
// Обработка события "Нажатие мыши на данные dataGrid_Orders" private void dataGrid_Orders_MouseUp(object sender, MouseButtonEventArgs e) { try { // Проверка на то, что клик мышки будет на колонке с данными if (dataGrid_Orders.CurrentCell.Column != null) { // При выборе строки получение ID заказа object item = dataGrid_Orders.SelectedItem; string ID_List_of_order_items = (dataGrid_Orders.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text; //MessageBox.Show(ID_List_of_order_items); //Debug.WriteLine((dataGrid_Orders.SelectedIndex).ToString()); // Загрузка данных в dataGrid_List_of_order_items using (OrdersdbEntities db = new OrdersdbEntities()) { var dataFromQuary = from o in db.Orders.AsEnumerable() from l in db.List_of_order_items from p in db.Products where o.orders_id == Convert.ToInt32(ID_List_of_order_items) where l.list_of_order_items__id == o.list_of_order_items where l.item_product_id == p.product_id select new { product_id = p.product_id, product_name = p.product_name, item_count = l.item_count, product_price = p.product_price, sum = l.item_count * p.product_price }; dataGrid_List_of_order_items.ItemsSource = dataFromQuary; } } } catch (Exception e2) { MessageBox.Show(e2.Message, "dataGrid_Orders_MouseUp", MessageBoxButton.OK, MessageBoxImage.Error); } }
public CreateOrder() { InitializeComponent(); // Загрузка списка товаров из базы в comboBox_nameProduct try { comboBox_nameProduct.Items.Add("Не выбрано"); using (OrdersdbEntities db = new OrdersdbEntities()) { var product = db.Products; foreach (Product u in product) { comboBox_nameProduct.Items.Add(u.product_name); } } comboBox_nameProduct.SelectedIndex = 0; } catch (Exception e) { MessageBox.Show(e.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void SaveEditOrder_Button_Click(object sender, RoutedEventArgs e) { try { using (OrdersdbEntities db = new OrdersdbEntities()) { // 1. Внесение изменений в таблицу Order if (TextBox_NameClient.Text != "") { var nameClient = db.Orders .Where(x => x.orders_id == orders_id_temp_EditOrder) .FirstOrDefault(); nameClient.name_client = TextBox_NameClient.Text; db.SaveChanges(); // 2. Внесение изменений в таблицу List_of_order_items // Удаление существующих записей с ID: list_of_order_items_temp_EditOrder // var list_of_order_items_temp = db.List_of_order_items // .Where(x => x.list_of_order_items__id == list_of_order_items_temp_EditOrder) // .FirstOrDefault(); //db.List_of_order_items.Remove(list_of_order_items_temp); //db.SaveChanges(); db.Database.ExecuteSqlCommand("DELETE FROM [dbo].[List_of_order_items] WHERE [list_of_order_items__id] = {0}", new object[] { list_of_order_items_temp_EditOrder }); // Выборка данных из List_of_order_items и запись в таблицу List_of_order_items //foreach (var item in dataGrid_EditOrder.Items.OfType<Item>()) //{ // List_of_order_items list_of_order_items = new List_of_order_items(); // list_of_order_items.list_of_order_items__id = list_of_order_items_temp_EditOrder; // list_of_order_items.item_product_id = item.product_id; // list_of_order_items.item_count = Convert.ToInt32(item.item_count); // db.List_of_order_items.Add(list_of_order_items); //} foreach (var u in col) { //dataGrid_EditOrder.Items.Add(new Item() { product_id = u.product_id, product_name = u.product_name, item_count = u.item_count1.ToString() }); List_of_order_items list_of_order_items = new List_of_order_items(); list_of_order_items.list_of_order_items__id = list_of_order_items_temp_EditOrder; list_of_order_items.item_product_id = db.Products.Where(x => x.product_name == u.product_name).Select(x => x.product_id).FirstOrDefault(); list_of_order_items.item_count = Convert.ToInt32(u.item_count); db.List_of_order_items.Add(list_of_order_items); } db.SaveChanges(); // Закрытие окна MessageBox.Show("Данные успешно изменены", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } else { MessageBox.Show("Поле \"ФИО клиента\" пустое", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } catch (Exception e1) { MessageBox.Show(e1.Message, "SaveEditOrder_Button_Click", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void CreateOrder_Button_Click(object sender, RoutedEventArgs e) { // id списка позиций клиента int list_of_order_itemsMAX = 0; try { using (OrdersdbEntities db = new OrdersdbEntities()) { // Получить максимальное число list_of_order_items list_of_order_itemsMAX = db.Orders.Select(x => x.list_of_order_items).Max(); } } catch (Exception e1) { MessageBox.Show(e1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } list_of_order_itemsMAX++; // Проверка заполнения полей формы if (NameClient_TextBox.Text == "" || dataGrid_List_of_order_items.Items.Count == 0) { MessageBox.Show("Введите в поле ФИО клиента значение или у Вас 0 позиций заказа", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else { try { // Выборка данных из dataGrid_List_of_order_items и запись в таблицу List_of_order_items foreach (var item in dataGrid_List_of_order_items.Items.OfType <Item>()) { List_of_order_items list_of_order_items = new List_of_order_items(); list_of_order_items.list_of_order_items__id = list_of_order_itemsMAX; list_of_order_items.item_product_id = item.product_id; list_of_order_items.item_count = Convert.ToInt32(item.item_count); using (OrdersdbEntities db = new OrdersdbEntities()) { db.List_of_order_items.Add(list_of_order_items); db.SaveChanges(); } } // Подготовка и запись в таблицу Order Order order = new Order(); order.date = DateTime.Now; order.name_client = NameClient_TextBox.Text; order.list_of_order_items = list_of_order_itemsMAX; using (OrdersdbEntities db = new OrdersdbEntities()) { db.Orders.Add(order); db.SaveChanges(); } MessageBox.Show("Данные успешно обновлены", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); // Обновить датагрид //MainWindow wnd = new MainWindow(); //wnd.Load_dataGrid_Orders(); // закрыть окно this.Close(); } catch (Exception e1) { MessageBox.Show(e1.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error); } } }