コード例 #1
0
ファイル: MainWindow.cs プロジェクト: vrekeda/dblab2
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //if (!f) return;

            AddWindow       addWindow;
            DataGridViewRow newRow;

            switch (curTable)
            {
            case "Clients":
                addWindow = new AddWindow(typeof(Client).GetProperties()
                                          .Select(property => property.Name)
                                          .ToList());
                addWindow.ShowDialog();
                if (addWindow.DialogResult == DialogResult.OK && addWindow.save)
                {
                    try
                    {
                        newRow = addWindow.newRow;
                        Client client = new Client();
                        client.Firstname = newRow.Cells[0].Value.ToString();
                        client.Lastname  = newRow.Cells[1].Value.ToString();
                        client.Birthdate = Convert.ToDateTime(newRow.Cells[2].Value.ToString());
                        client.Gender    = Convert.ToBoolean(newRow.Cells[3].Value.ToString());
                        db.Clients.Add(client);
                        db.SaveChanges();
                        FillTableGridInfo(curTable);
                    }
                    catch (Exception ex)
                    {
                        ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                        errorWindow.Show();
                    }
                }
                break;

            case "Tours":
                addWindow = new AddWindow(typeof(Tour).GetProperties()
                                          .Select(property => property.Name)
                                          .ToList());
                addWindow.ShowDialog();
                if (addWindow.DialogResult == DialogResult.OK && addWindow.save)
                {
                    try
                    {
                        newRow = addWindow.newRow;
                        Tour tour = new Tour();
                        tour.Price       = decimal.Parse(newRow.Cells[0].Value.ToString());
                        tour.Tourdate    = Convert.ToDateTime(newRow.Cells[1].Value.ToString());
                        tour.City        = newRow.Cells[2].Value.ToString();
                        tour.IdTA        = Int32.Parse(newRow.Cells[3].Value.ToString());
                        tour.Description = newRow.Cells[4].Value.ToString();
                        db.Tours.Add(tour);
                        db.SaveChanges();
                        FillTableGridInfo(curTable);
                    }
                    catch (Exception ex)
                    {
                        ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                        errorWindow.Show();
                    }
                }
                break;

            case "TravelAgencies":
                addWindow = new AddWindow(typeof(TravelAgency).GetProperties()
                                          .Select(property => property.Name)
                                          .ToList());
                addWindow.ShowDialog();
                if (addWindow.DialogResult == DialogResult.OK && addWindow.save)
                {
                    try
                    {
                        newRow = addWindow.newRow;
                        TravelAgency travelAgency = new TravelAgency();
                        travelAgency.Name   = newRow.Cells[0].Value.ToString();
                        travelAgency.Adress = newRow.Cells[1].Value.ToString();
                        db.TravelAgencies.Add(travelAgency);
                        db.SaveChanges();
                        FillTableGridInfo(curTable);
                    }
                    catch (Exception ex)
                    {
                        ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                        errorWindow.Show();
                    }
                }
                break;
            }
        }
コード例 #2
0
ファイル: MainWindow.cs プロジェクト: vrekeda/dblab2
        private void TableDataView_Click(object sender, DataGridViewCellEventArgs e)
        {
            //if (!f) return;
            if (e.ColumnIndex == TableDataGrid.ColumnCount - 2)
            {
                EditWindow editWindow = new EditWindow(TableDataGrid.Rows[e.RowIndex], typeof(Client).GetProperties()
                                                       .Select(property => property.Name)
                                                       .ToList());
                editWindow.ShowDialog();
                if (editWindow.save)
                {
                    DataGridViewRow newRow = editWindow.editedRow;
                    switch (curTable)
                    {
                    case "Clients":
                        try
                        {
                            Client client = db.Clients
                                            .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                            .FirstOrDefault();
                            client.Firstname = newRow.Cells[0].Value.ToString();
                            client.Lastname  = newRow.Cells[1].Value.ToString();
                            client.Birthdate = Convert.ToDateTime(newRow.Cells[2].Value.ToString());
                            client.Gender    = Convert.ToBoolean(newRow.Cells[3].Value.ToString());
                            db.SaveChanges();
                            FillTableGridInfo("Clients");
                        }
                        catch (Exception ex)
                        {
                            ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                            errorWindow.Show();
                        }
                        break;

                    case "Tours":
                        try
                        {
                            Tour tour = db.Tours
                                        .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                        .FirstOrDefault();
                            tour.Price       = decimal.Parse(newRow.Cells[0].Value.ToString());
                            tour.Tourdate    = Convert.ToDateTime(newRow.Cells[1].Value.ToString());
                            tour.City        = newRow.Cells[2].Value.ToString();
                            tour.IdTA        = Int32.Parse(newRow.Cells[3].Value.ToString());
                            tour.Description = newRow.Cells[4].Value.ToString();
                            db.SaveChanges();
                            FillTableGridInfo("Tours");
                        }
                        catch (Exception ex)
                        {
                            ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                            errorWindow.Show();
                        }
                        break;

                    case "TravelAgencies":
                        try
                        {
                            TravelAgency travelAgency = db.TravelAgencies
                                                        .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                                        .FirstOrDefault();
                            travelAgency.Name   = newRow.Cells[0].Value.ToString();
                            travelAgency.Adress = newRow.Cells[1].Value.ToString();
                            db.SaveChanges();
                            FillTableGridInfo("TravelAgencies");
                        }
                        catch (Exception ex)
                        {
                            ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                            errorWindow.Show();
                        }
                        break;
                    }
                }
            }
            else
            if (e.ColumnIndex == TableDataGrid.ColumnCount - 1)
            {
                DeleteWindow deleteWindow = new DeleteWindow();
                if (deleteWindow.ShowDialog() == DialogResult.OK && deleteWindow.delete)
                {
                    try
                    {
                        switch (curTable)
                        {
                        case "Clients":
                            Client client = db.Clients
                                            .Where(c => c.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                            .FirstOrDefault();
                            db.Clients.Remove(client);
                            db.SaveChanges();
                            FillTableGridInfo("Clients");
                            break;

                        case "Tours":
                            db.Tours.Remove(db.Tours
                                            .Where(t => t.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                            .FirstOrDefault());
                            db.SaveChanges();
                            FillTableGridInfo("Tours");
                            break;

                        case "TravelAgencies":
                            db.TravelAgencies.Remove(db.TravelAgencies
                                                     .Where(ta => ta.Id == Int32.Parse(TableDataGrid.Rows[e.RowIndex].Cells[0].Value.ToString()))
                                                     .FirstOrDefault());
                            db.SaveChanges();
                            FillTableGridInfo("TravelAgencies");
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorWindow errorWindow = new ErrorWindow(ex.Message);
                        errorWindow.Show();
                    }
                }
            }
        }