-
Notifications
You must be signed in to change notification settings - Fork 0
/
PetManagerForm.cs
68 lines (60 loc) · 2.66 KB
/
PetManagerForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PetManager {
public partial class PetManagerForm : Form {
public PetManagerForm() {
InitializeComponent();
this.dgvPerson.DataSource = Person.GetDemoData();
this.dgvPerson.Rows[0].Selected = true;
}
private void dgvPerson_SelectionChanged(object sender, EventArgs e) {
var dgv = (DataGridView) sender;
if (dgv.SelectedRows.Count > 0) {
var person = (Person)dgv.SelectedRows[0].DataBoundItem;
this.dgvPet.DataSource = person.Pets;
}
}
private void btnAddPerson_Click(object sender, EventArgs e) {
var personList = (BindingList<Person>) this.dgvPerson.DataSource;
personList.Add(new Person());
this.dgvPerson.Refresh();
}
private void btnRemovePerson_Click(object sender, EventArgs e) {
if (this.dgvPerson.SelectedRows.Count > 0) {
var person = (Person)this.dgvPerson.SelectedRows[0].DataBoundItem;
var personList = (BindingList<Person>)this.dgvPerson.DataSource;
personList.Remove(person);
} else {
MessageBox.Show("You have to select a person first.", "Remove person", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnAddPet_Click(object sender, EventArgs e) {
if (this.dgvPerson.SelectedRows.Count > 0) {
var person = (Person)this.dgvPerson.SelectedRows[0].DataBoundItem;
person.Pets.Add(new Pet());
} else {
MessageBox.Show("You have to select a person first.", "Add pet", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnRemovePet_Click(object sender, EventArgs e) {
if (this.dgvPet.SelectedRows.Count > 0) {
var person = (Person)this.dgvPerson.SelectedRows[0].DataBoundItem;
var pet = (Pet)this.dgvPet.SelectedRows[0].DataBoundItem;
person.Pets.Remove(pet);
} else {
MessageBox.Show("You have to select a pet first.", "Remove pet", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnShowStatistic_Click(object sender, EventArgs e) {
var personList = (BindingList<Person>)this.dgvPerson.DataSource;
new StatisticsForm(personList).ShowDialog(this);
}
}
}