From d4ec177bb2eafaf22de79b04f67ec16625ae18c9 Mon Sep 17 00:00:00 2001 From: Iamgracee Date: Mon, 30 Mar 2026 06:30:43 +0800 Subject: [PATCH] Update Form1.cs --- TestWins/Form1.cs | 80 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..a877a22 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,109 @@ -using System.Drawing.Text; using TestWins.Controller; +using TestWins.Model; namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); - loadData(); + + try + { + loadData(); + } + catch (Exception ex) + { + MessageBox.Show("Error loading data: " + ex.Message); + } } private void loadData() { + dataGridView1.DataSource = null; dataGridView1.DataSource = controller.getAll(); } private void btnAdd_Click(object sender, EventArgs e) { + try + { + Student s = new Student + { + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.createStudent(s); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Add failed: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + Student s = new Student + { + studentId = int.Parse(txtStudentId.Text), + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.update(s); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Update failed: " + ex.Message); + } } private void btnDelete_Click(object sender, EventArgs e) { + try + { + int id = int.Parse(txtStudentId.Text); + controller.delete(id); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Delete failed: " + ex.Message); + } + } + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex >= 0) + { + DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; + txtStudentId.Text = row.Cells[0].Value.ToString(); + txtName.Text = row.Cells[1].Value.ToString(); + txtAge.Text = row.Cells[2].Value.ToString(); + txtCourse.Text = row.Cells[3].Value.ToString(); + } } - private void dataGridView1_CellClick(object sender, EventArgs e) + private void clearFields() { - + txtStudentId.Text = ""; + txtName.Text = ""; + txtAge.Text = ""; + txtCourse.Text = ""; } -} +} \ No newline at end of file