Navigate Record In the DataGridView Control (Count the records, First and Last Buttons)

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. firstform 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.
  1. 'storing the max value in the label(lbl_max)
  2. lbl_max.Text = max - 2
  3. 'storing the first value in the label(lbl_start)
  4. lbl_start.Text = 0
In the 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.
  1. 'passing the value of incVal into the label(lbl_start)
  2. lbl_start.Text = incVal
Go back to the DesignViews, double click the first button and do this code for the first displayed records in the DataGridView.
  1. Private Sub btn_first_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_first.Click
  2.  
  3. Try
  4. 'declaring the variable that count the
  5. 'number of records and it will be displayed in the datagridview
  6. Dim resultPerPage As Integer = 5
  7. 'declaring the variable for the first list
  8. 'of records that will display in the datagridview
  9. Dim startResult As Integer
  10.  
  11. 'setting up the increment value which is the variable incVal to zero
  12. 'so that if you click the button, it return to original value
  13. incVal = 0
  14. 'formula of the list of records that will display in the datagridview
  15. startResult = incVal * resultPerPage
  16.  
  17. 'passing the value of incVal into the label(lbl_start)
  18. lbl_start.Text = incVal
  19.  
  20.  
  21. con.Open()
  22. 'store the query with the limit to a string variable
  23. sql = "Select * from employees limit " & startResult & "," & resultPerPage & ""
  24.  
  25. 'set a new spicific table in the database
  26. dt = New DataTable
  27. 'set your commands for holding the data.
  28. With cmd
  29. .Connection = con
  30. .CommandText =sql
  31. End With
  32. 'filling the table in the database
  33. da = New MySqlDataAdapter(sql, con)
  34. da.Fill(dt)
  35. 'getting the datasource that will display on the datagridview
  36. DataGridView1.DataSource = dt
  37.  
  38.  
  39. Catch ex As Exception
  40. MsgBox(ex.Message)
  41. End Try
  42. 'closing connection
  43. con.Close()
  44. End Sub
Go back to the DesignViews again, double click the last button and do this code for the last displayed records in the DataGridView.
  1. Private Sub btn_last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_last.Click
  2.  
  3. Try
  4. 'declaring the variable that count the
  5. 'number of records and it will be displayed in the datagridview
  6. Dim resultPerPage As Integer = 5
  7. 'declaring the variable for the first list
  8. 'of records that will display in the datagridview
  9. Dim startResult As Integer
  10.  
  11. 'setting up the increment value which is the
  12. 'variable incVal to the total rows
  13. 'in the table of the database
  14. incVal = max - 2
  15. 'formula of the list of records that will display in the datagridview
  16. startResult = incVal * resultPerPage
  17.  
  18. 'passing the value of incVal into the label(lbl_start)
  19. lbl_start.Text = incVal
  20.  
  21.  
  22. con.Open()
  23. 'store the query with the limit to a string variable
  24. sql = "Select * from employees limit " & startResult & "," & resultPerPage & ""
  25.  
  26. 'set a new spicific table in the database
  27. dt = New DataTable
  28. 'set your commands for holding the data.
  29. With cmd
  30. .Connection = con
  31. .CommandText = sql
  32. End With
  33. 'filling the table in the database
  34. da = New MySqlDataAdapter(sql, con)
  35. da.Fill(dt)
  36. 'getting the datasource that will display on the datagridview
  37. DataGridView1.DataSource = dt
  38.  
  39.  
  40. Catch ex As Exception
  41. MsgBox(ex.Message)
  42. End Try
  43. 'closing connection
  44. con.Close()
  45. End Sub
Complete Source Code is included. You download and run it on your computer.

Add new comment