Professional Documents
Culture Documents
dangminhthang@tdtu.edu.vn
October 2021
1
Table of Content
I – Objectives ............................................................................................................................................... 2
II – Exercise .................................................................................................................................................. 2
III – Exercise Guide ...................................................................................................................................... 3
3.1 Import database ................................................................................................................................ 3
3.2 Implementation ................................................................................................................................ 4
3.2.1 Connect DataGridView to Database ........................................................................................ 4
3.2.2 Handle cell click event ............................................................................................................. 13
3.2.3 Handle “Update” button ......................................................................................................... 14
3.2.4 Handle “Delete” button .......................................................................................................... 15
3.2.5 Handle “Add” button............................................................................................................... 15
3.2.6 Handle “Clear” button............................................................................................................. 15
2
I – Objectives
The objective of this lesson is to build a simple Windows Forms Applications with database
connection and manipulation. By the end of this lesson, you’ll be able to:
II – Exercise
First, design a Windows Forms Application with the following Graphical User Interface
(GUI):
Second, use the provided lab02.sql to create a database needed for this lab. After running
that file, you will see a school database in which you will see a student table containing
some students.
Requirements:
• After the application is loaded, display the list of students in the DataGridView.
• If a student is selected in the DataGridView, the details of that student will be
presented in the inputs (textboxes, radio button and date time picker). If user changes
some information and clicks “Update”, the new information will be updated in the
DataGridView and saved into database.
• Clicking on button “Delete” will delete the student currently selected in the
DataGridView and in database. After delete, the values of textboxes, radio button
and date time picker will be reset to default values.
• Clicking on button “Clear” will clear selection in the DataGridView and reset the
values of textboxes, radio button and date time picker.
• Clicking on button “Add” will add a new student in the DataGridView and in
database.
3
Additional requirements:
Copy the content of the file “lab02.sql” into the query window and click “Execute”:
4
If successful, you will see a “school” database with a “student” table (you may have to click
on “Refresh” button to see the database):
3.2 Implementation
3.2.1 Connect DataGridView to Database
Select the DataGridView and click on the small arrow icon on the top right corner:
5
From the Choose Data Source dropdown, click on “Add Project Data Source”:
6
In the “Add Connection” screen, enter your Server name (you can find your Server name in
the Microsoft SQL Server Management Studio), and choose “school” in the database name:
10
Now there is a generated file called schoolDataSet.designer.cs, in which you can see a
schoolDataSet class and a studentDataTable class:
13
Then you can write code to display the details of the selected student in the form:
14
Then you can write code to update the details of the selected student in the form:
15