public EditForm(int id, Form1 mainForm) { InitializeComponent(); this.id = id; this.mainForm = mainForm; using (var ctx = new Model1Container()) { /* var selectedAd = from ad in ctx.BargainAdsSet where ad.Id == id select new { ad.Id, ad.Brand, ad.Model, ad.Engine, ad.Year, ad.Price, ad.City, ad.FromDate, ad.ToDate }; */ var selectedAd = ctx.BargainAdsSet.First(i => i.Id == id); idLabel.Text = id.ToString(); brandTxt.Text = selectedAd.Brand; modelTxt.Text = selectedAd.Model; engineTxt.Text = selectedAd.Engine; yearTxt.Text = selectedAd.Year; priceTxt.Text = selectedAd.Price; cityTxt.Text = selectedAd.City; dateFrom.Text = selectedAd.FromDate; dateTo.Text = selectedAd.ToDate; } }
public Form1() { InitializeComponent(); source = new BindingSource(); using (var ctx = new Model1Container()) { var ads = from carAds in ctx.BargainAdsSet select new { carAds.Id, carAds.Brand, carAds.Model, carAds.Engine, carAds.Year, carAds.Price, carAds.City, carAds.FromDate, carAds.ToDate }; source.ResetBindings(true); source.DataSource = ads.ToList(); try { dataGridView1.DataSource = source; } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } }
private void submitBtn_Click(object sender, EventArgs e) { using (var ctx = new Model1Container()) { try { var editedAd = ctx.BargainAdsSet.First(i => i.Id == id); editedAd.Brand = brandTxt.Text; editedAd.Model = modelTxt.Text; editedAd.Engine = engineTxt.Text; editedAd.Year = yearTxt.Text; editedAd.Price = priceTxt.Text; editedAd.City = cityTxt.Text; editedAd.FromDate = dateFrom.Value.ToShortDateString(); editedAd.ToDate = dateTo.Value.ToShortDateString(); ctx.SaveChanges(); try { var ads = from carAds in ctx.BargainAdsSet select new { carAds.Id, carAds.Brand, carAds.Model, carAds.Engine, carAds.Year, carAds.Price, carAds.City, carAds.FromDate, carAds.ToDate }; mainForm.source.ResetBindings(true); mainForm.source.DataSource = ads.ToList(); mainForm.dataGridView1.DataSource = mainForm.source; this.Close(); } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } }
private void deleteBtn_Click(object sender, EventArgs e) { int row = dataGridView1.CurrentCell.RowIndex; int id = (int)dataGridView1["Id", row].Value; using (var ctx = new Model1Container()) { try { var selectedAd = ctx.BargainAdsSet.First(i => i.Id == id); ctx.BargainAdsSet.Remove(selectedAd); ctx.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.InnerException); } try { var ads = from carAds in ctx.BargainAdsSet select new { carAds.Id, carAds.Brand, carAds.Model, carAds.Engine, carAds.Year, carAds.Price, carAds.City, carAds.FromDate, carAds.ToDate }; source.ResetBindings(true); source.DataSource = ads.ToList(); dataGridView1.DataSource = source; } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } }
private void submitBtn_Click(object sender, EventArgs e) { using (var ctx = new Model1Container()) { try { BargainAds newCarAd = new BargainAds { Brand = brandTxt.Text, Model = modelTxt.Text, Engine = engineTxt.Text, Year = yearTxt.Text, Price = priceTxt.Text, City = cityTxt.Text, FromDate = dateFrom.Value.ToShortDateString(), ToDate = dateTo.Value.ToShortDateString() }; ctx.BargainAdsSet.Add(newCarAd); ctx.SaveChanges(); try { var ads = from carAds in ctx.BargainAdsSet select new { carAds.Id, carAds.Brand, carAds.Model, carAds.Engine, carAds.Year, carAds.Price, carAds.City, carAds.FromDate, carAds.ToDate }; source.ResetBindings(true); source.DataSource = ads.ToList(); dataGridView1.DataSource = source; } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } catch (Exception ex) { Console.WriteLine(ex.InnerException); } } }