You are on page 1of 16

`

502045 – Software Engineering Lab 02

Prepared by Dang Minh Thang

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:

• Create a connection to Microsoft SQL server


• Use DataGridView
• Handle CRUD (Create, Read, Update, Delete) actions

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:

• If a student is selected in the DataGridView, disable button “Add” because at this


point the textboxes, radio button and date time picker are displaying the details of
current selected student. To enable “Add” button, user must click on the “Clear”
button first.
• Similarly, if no student is selected in the DataGridView, the “Update” and “Delete”
buttons must be disabled.
• Disable add, update, delete features of the DataGridView. All actions must be done
via the “Add”, “Delete”, “Update” button.

III – Exercise Guide


3.1 Import database
Open Microsoft SQL Server Management Studio, and click on “New Query” button:

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 next screen, select “Database” and click Next:


7

Then select “Dataset” and click Next:


8

In the next screen, click on “New Connection”:


9

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

Click “OK” and then Click “Next”:


11

Select “student” table and click “Finish”:


12

If everything is successful, you will see the DataGridView as follows:

And you run your application, you will see this:

Now there is a generated file called schoolDataSet.designer.cs, in which you can see a
schoolDataSet class and a studentDataTable class:
13

3.2.2 Handle cell click event


In the designer view, select the DataGridView, and in the properties window select the
Events tab and double click on “CellClick” event:

Then you can write code to display the details of the selected student in the form:
14

3.2.3 Handle “Update” button


In the designer view, select the “Update” button and double click on “Click” event:

Then you can write code to update the details of the selected student in the form:
15

3.2.4 Handle “Delete” button

3.2.5 Handle “Add” button

3.2.6 Handle “Clear” button

You might also like