private void bunifuImageButton2_Click(object sender, EventArgs e) { using (SearchProductsEntities db = new SearchProductsEntities()) { var remove = db.ShopsAdress.Where(x => x.ShopAdress == SearchDataGrid.CurrentCell.Value.ToString()).FirstOrDefault(); db.ShopsAdress.Remove(remove); db.SaveChanges(); } successLabel.Visible = true; successLabel.Text = "Адрес удален"; successLabel.ForeColor = Color.Red; SearchDataGrid.Rows.RemoveAt(SearchDataGrid.CurrentCell.RowIndex); }
private void searchButton_Click(object sender, EventArgs e) { SearchDataGrid.Rows.Clear(); errorLabel.Visible = false; successLabel.Visible = true; string product_to_search = bunifuMetroTextbox1.Text; #region Обработка ввода пустых строк if (String.IsNullOrEmpty(product_to_search)) //Проверка на ввод пустых значений { errorLabel.Text = "Введите название товара"; errorLabel.Visible = true; return; } #endregion #region Запрос к БД на поиск using (SearchProductsEntities db = new SearchProductsEntities()) { var results = from products in db.Products where products.ProductName.Contains(bunifuMetroTextbox1.Text) join manufacturers in db.Manufacturer on products.ProductManufacturer equals manufacturers.Manufacturer_id join categories in db.Categories on products.ProductCategory equals categories.Category_id join prices in db.Prices on products.Product_id equals prices.Product_id join shops in db.Shops on prices.Shop_id equals shops.Id where shops.ShopName == "Семья" select new { product_name = products.ProductName, product_category = categories.CategoryName, product_manufacturer = manufacturers.ManufacturerName, product_volume = products.ProductVolume, product_price = prices.ProductPrice }; #region Запрос не найден if (results.Count() == 0) { errorLabel.Text = "По данному запросу ничего не найдено"; errorLabel.Visible = true; } #endregion #region Добавление в таблицу foreach (var res in results) { SearchDataGrid.Rows.Add(res.product_name, res.product_category, res.product_manufacturer, res.product_volume, res.product_price); } #endregion } #endregion }
private void SearchDataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { if (SearchDataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) { using (SearchProductsEntities db = new SearchProductsEntities()) { var res = db.ShopsAdress.Where(x => x.ShopAdress == SearchDataGrid.CurrentCell.Value.ToString()).FirstOrDefault(); if (res == null) { var shop_id = db.Shops.Where(x => x.ShopName == "Семья").Select(x => x.Id).FirstOrDefault(); ShopsAdress sa = new ShopsAdress(); sa.Shop_id = shop_id; sa.ShopAdress = SearchDataGrid.CurrentCell.Value.ToString(); db.ShopsAdress.Add(sa); db.SaveChanges(); successLabel.Text = "Адрес успешно добавлен"; successLabel.ForeColor = Color.ForestGreen; successLabel.Visible = true; } else { successLabel.Visible = true; successLabel.Text = "Адрес уже добавлен"; successLabel.ForeColor = Color.Red; SearchDataGrid.Rows.RemoveAt(e.RowIndex); } } } else { successLabel.Visible = true; successLabel.Text = "Укажите адрес"; successLabel.ForeColor = Color.Red; SearchDataGrid.Rows.RemoveAt(e.RowIndex); } } catch { } }
public ShopsListAdmin() { InitializeComponent(); using (SearchProductsEntities db = new SearchProductsEntities()) { var results = db.Shops.Join(db.ShopsAdress, // второй набор p => p.Id, // свойство-селектор объекта из первого набора c => c.Shop_id, // свойство-селектор объекта из второго набора (p, c) => new { shop_name = p.ShopName, shop_adress = c.ShopAdress, shop_lat = c.ShopLatitude, shop_lon = c.ShopLongitude }).Where(x => x.shop_name == "Семья"); foreach (var shop in results) { SearchDataGrid.Rows.Add(shop.shop_name, shop.shop_adress); } } }