Navigate Record In the DataGridView Control (Count the records, First and Last Buttons)
Submitted by janobe on Sunday, January 26, 2014 - 23:17.
This is the continuation of my previous tutorial which is the Navigate Record In the DataGridView Control. This time I’m going to add the last and first buttons and at the same time I’m going to add the counting of how many times the displayed records change in the DataGridView. For you, to know the limit in changing of displaying records in the DataGridView.
Let’s Begin:
Open your Visual Basic 2008, open your file which is the Navigate Record In the DataGridView Control and add two buttons, and three labels. And it will look like this.
Double click the Form and set up the first Label(lbl_start) that represents the start value. And then, set up the second Label(lbl_max) that represent a max value in changing the displayed records in the DataGridView.
In the
Go back to the DesignViews, double click the first button and do this code for the first displayed records in the DataGridView.
Go back to the DesignViews again, double click the last button and do this code for the last displayed records in the DataGridView.
Complete Source Code is included. You download and run it on your computer.

- 'storing the max value in the label(lbl_max)
- lbl_max.Text = max - 2
- 'storing the first value in the label(lbl_start)
- lbl_start.Text = 0
btn_next_Click
, do this code to increment the Label(lbl_start). And at the same time do it on prev_Click
for decreasing the value. Put this code, under the formula.
- 'passing the value of incVal into the label(lbl_start)
- lbl_start.Text = incVal
- Private Sub btn_first_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_first.Click
- Try
- 'declaring the variable that count the
- 'number of records and it will be displayed in the datagridview
- Dim resultPerPage As Integer = 5
- 'declaring the variable for the first list
- 'of records that will display in the datagridview
- Dim startResult As Integer
- 'setting up the increment value which is the variable incVal to zero
- 'so that if you click the button, it return to original value
- incVal = 0
- 'formula of the list of records that will display in the datagridview
- startResult = incVal * resultPerPage
- 'passing the value of incVal into the label(lbl_start)
- lbl_start.Text = incVal
- con.Open()
- 'store the query with the limit to a string variable
- sql = "Select * from employees limit " & startResult & "," & resultPerPage & ""
- 'set a new spicific table in the database
- dt = New DataTable
- 'set your commands for holding the data.
- With cmd
- .Connection = con
- .CommandText =sql
- End With
- 'filling the table in the database
- da = New MySqlDataAdapter(sql, con)
- da.Fill(dt)
- 'getting the datasource that will display on the datagridview
- DataGridView1.DataSource = dt
- Catch ex As Exception
- End Try
- 'closing connection
- End Sub
- Private Sub btn_last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_last.Click
- Try
- 'declaring the variable that count the
- 'number of records and it will be displayed in the datagridview
- Dim resultPerPage As Integer = 5
- 'declaring the variable for the first list
- 'of records that will display in the datagridview
- Dim startResult As Integer
- 'setting up the increment value which is the
- 'variable incVal to the total rows
- 'in the table of the database
- incVal = max - 2
- 'formula of the list of records that will display in the datagridview
- startResult = incVal * resultPerPage
- 'passing the value of incVal into the label(lbl_start)
- lbl_start.Text = incVal
- con.Open()
- 'store the query with the limit to a string variable
- sql = "Select * from employees limit " & startResult & "," & resultPerPage & ""
- 'set a new spicific table in the database
- dt = New DataTable
- 'set your commands for holding the data.
- With cmd
- .Connection = con
- .CommandText = sql
- End With
- 'filling the table in the database
- da = New MySqlDataAdapter(sql, con)
- da.Fill(dt)
- 'getting the datasource that will display on the datagridview
- DataGridView1.DataSource = dt
- Catch ex As Exception
- End Try
- 'closing connection
- End Sub
Add new comment
- 475 views