-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectStudentForm.cs
More file actions
72 lines (66 loc) · 2.62 KB
/
SelectStudentForm.cs
File metadata and controls
72 lines (66 loc) · 2.62 KB
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
69
70
71
72
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
namespace TinyCollegeGUI
{
public partial class SelectStudentForm : Form
{
private List<Student> students;
private string connectionString;
public SelectStudentForm()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["TinyCollegeDB"].ConnectionString;
LoadStudents();
}
private void LoadStudents()
{
// Generates list of students in database
students = new List<Student>();
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
// Tells system to SELECT info (student name, ID, and GPA) FROM the Students table from the database
string query = "SELECT StudentID, FirstName, LastName, GPA FROM Students";
using (var command = new SqlCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
students.Add(new Student
{
StudentId = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
GPA = reader.GetDouble(3)
});
}
}
}
}
dataGridViewStudents.DataSource = students;
}
// Event handler for Edit Selected Student button
private void btnEditSelectedStudent_Click(object sender, EventArgs e)
{
if (dataGridViewStudents.SelectedRows.Count > 0)
{
var selectedStudentId = dataGridViewStudents.SelectedRows[0].Cells["StudentId"].Value.ToString();
EditStudentForm editStudentForm = new EditStudentForm(selectedStudentId);
editStudentForm.ShowDialog();
this.Close();
}
else
{
// Displays message box to user to prompt them to select a student from the database whose information they wish to edit/update in the system
MessageBox.Show("Please select a student to edit.");
}
}
private void dataGridViewStudents_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}