Displaying and Counting the Total Value of the Records in the DataGridView
Submitted by janobe on Wednesday, January 22, 2014 - 10:03.
In this tutorial I will show you how to display and sum up the total values of the records in the DataGridView. The records that are displayed are items that has its price, then, the total value will be displayed in the last row of the DataGridView . I’m using MySQL Database as my database and Visual Basic 2008.
The features of this is to calculate the salaries of all employees
Let’s begin:
First, create a database named “payroll”.
Second, create a table named “employees”.
Open Visual Basic 2008, create a Project and set up you Form just like this.
Double click the Form. Set up your connection and declare all the classes that you needed above the
Go back the Design Views and double click the “Load” Button. After that, do these codes in the
Go back the Design Views and double click the “Calculate” Button. After that, do these codes in the
Third, insert all data in the database.
- (102, 'lex', 'De Haan', 17000),
- (103, 'alexander', 'Hunold', 9000),
- (104, 'Bruce', 'Ernst', 6000),
- (107, 'Diana', 'Lorents',4200),
- (124, 'Kevin', 'Mourgos', 5800),
- (141, 'Trenne', 'Rajs', 3500),
- (142, 'Curtis', 'Davies', 3100),
- (143, 'Randal', 'Matos', 2600),
- (144, 'Peter', 'Vargas', 2500),
- (149, 'Ellen', 'Zlotkey', 10500),
- (174, 'Jonathan', 'Abel', 11000),
- (176, 'Kimberly', 'Taylor', 8600),
- (178, 'Jinnefer', 'Grant', 7000),
- (200, 'Michael', 'Whalen', 4400),
- (201, 'Pat', 'Hartstein', 13000),
- (205, 'Shelley', 'Fay', 6000),
- (206, 'William', 'Higgins', 12000),
- (207, 'hatch', 'Glets', 8300);
Form1_Load
.
- 'reference
- Imports MySql.Data.MySqlClient
- Public Class Form1
- 'setting up the string connection of MySQL Database
- Dim con As MySqlConnection = _
- New MySqlConnection("server=localhost;user id=root;database=payroll")
- 'a set of command in MySQL
- Dim cmd As New MySqlCommand
- 'Bridge between a database and the datatable for retrieving and saving data.
- Dim da As New MySqlDataAdapter
- 'a specific table in the database
- Dim dt As New DataTable
display_Click
.
- Private Sub display_Click _
- (ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Button1.Click
- 'for displaying records in the datagridview from the database
- Try
- 'openning connection
- con.Open()
- 'set a new spicific table in the database
- dt = New DataTable
- 'set your commands for holding the data.
- With cmd
- .Connection = con
- .CommandText = "Select * from employees"
- End With
- 'filling the table in the database
- da = New MySqlDataAdapter("Select * from employees", con)
- da.Fill(dt)
- 'getting the datasource that will display on the datagridview
- DataGridView1.DataSource = dt
- 'declaring variable as integer to store the value of the total rows in the datagridview
- Dim max As Integer = DataGridView1.Rows.Count - 1
- 'putting the string value in the last row of the datagridview
- DataGridView1.Rows(max).Cells(2).Value = "Total"
- 'putting the value which is 0 in the last row of the datagridview
- 'this is the first value that appear to the last row in the column 3
- 'this is for avoiding error when you begin calculating
- DataGridView1.Rows(max).Cells(3).Value = 0
- Catch ex As Exception
- End Try
- 'closing connection
- End Sub
Calculate_Click
. This is for calculating all the value in the rows.
- Private Sub Calculate_Click _
- (ByVal sender As System.Object, ByVal e As System.EventArgs) _
- Handles Button2.Click
- Try
- 'declaring variable as integer to store the value of the total rows in the datagridview
- Dim max As Integer = DataGridView1.Rows.Count - 1
- 'getting the values of a specific rows
- For Each row As DataGridViewRow In DataGridView1.Rows
- 'formula for adding the values in the rows
- DataGridView1.Rows(max).Cells(3).Value += row.Cells(3).Value
- Next
- Catch ex As Exception
- End Try
- End Sub
Add new comment
- 459 views