コード例 #1
0
ファイル: Form1.cs プロジェクト: Seishin/car-bargain-ads
        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);
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: Seishin/car-bargain-ads
        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);
                }
            }
        }