private void RandFillBtn_Click(object sender, EventArgs e) { RandomFilling randomFilling = new RandomFilling(db); switch (curTable) { case "Clients": for (int i = 0; i < 10; i++) { try { Client client = randomFilling.GetRandClient(); db.Clients.Add(client); db.SaveChanges(); ClientToTour clientToTour = new ClientToTour() { ClientId = client.Id, TourId = randomFilling.GetRandomTourId() }; db.ClientToTours.Add(clientToTour); client.ClientToTour.Add(clientToTour); db.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } FillTableGridInfo("Clients"); break; case "Tours": for (int i = 0; i < 10; i++) { Tour tour = randomFilling.GetRandTour(); db.Tours.Add(tour); db.SaveChanges(); } FillTableGridInfo("Tours"); break; case "TravelAgencies": for (int i = 0; i < 10; i++) { TravelAgency travelAgency = randomFilling.GetRandTravelAgency(); db.TravelAgencies.Add(travelAgency); db.SaveChanges(); TravelAgencyToClient travelAgencyToClient = new TravelAgencyToClient() { ClientId = randomFilling.GetRandomClientId(), TravelAgencyId = travelAgency.Id }; db.TravelAgencyToClients.Add(travelAgencyToClient); travelAgency.TravelAgencyToClient.Add(travelAgencyToClient); db.SaveChanges(); } FillTableGridInfo("TravelAgencies"); break; } }
public TravelAgency GetRandTravelAgency() { TravelAgency travelAgency = new TravelAgency() { Name = randData["compname"][random.Next(randData["compname"].Count)], Adress = randData["address"][random.Next(randData["address"].Count)], }; return(travelAgency); }
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; } }
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(); } } } }