Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 73 additions & 7 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -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 = "";
}
}
}