How to Create a Simple Record Navigation in C#

This tutorial is a continuation of our last topic called “How to Display Selected Row from Datagridview into Textbox using C#”. But this time, I’m going to show you how to create a simple record navigation using C#. This will help the user to navigate through all the record from the database easily. To start this application open our last project called “student_info”, then add another button and arrange it that looks like as shown below. d1 Then we will add two lines of code below this line of code OleDbConnection conn = new OleDbConnection(connection);. In this new line of code, we are just simply declaring an integer variable that has an initial value of zero.
  1. int curRecord = 0;
  2. int totRecord = 0;
Next, we are going to create a subroutine called “Navigate()”. We call this subroutine when we navigate through the datatable. And this subroutine will be called when the previous and next button is clicked. And here’s the code for this subroutine.
  1. private void Navigate()
  2. {
  3. txtid.Text = dt.Rows[curRecord][0].ToString();
  4. txtfname.Text = dt.Rows[curRecord][1].ToString();
  5. txtlname.Text = dt.Rows[curRecord][2].ToString();
  6. txtcourse.Text = dt.Rows[curRecord][3].ToString();
  7. txtgender.Text = dt.Rows[curRecord][4].ToString();
  8. txtaddress.Text = dt.Rows[curRecord][5].ToString();
  9. }
This time we proceed to the “Load All Data” button and let’s modify the by adding new lines of code. And then here’s the new modified code for “Load All Data” button.
  1. private void btnload_Click(object sender, EventArgs e)
  2. {
  3. //create a new datatable
  4. dt = new DataTable();
  5. //create our SQL SELECT statement
  6. string sql = "Select * from tblstudent";
  7. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  8. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  9. //we fill the result to dt which declared above as datatable
  10. da.Fill(dt);
  11.  
  12. //set the curRecord to zero
  13. curRecord = 0;
  14. //get the total number of record available in the database and stored to totRecord Variable
  15. totRecord = dt.Rows.Count;
  16.  
  17.  
  18. //then we populate the datagridview by specifying the datasource equal to dt
  19. dataGridView1.DataSource = dt;
  20. }
Next, we will add a code for our Previous button.this previous will simply do the decrementing the row number of the Datatable by one. And here’s the code for “Previous” button.
  1. private void btnprev_Click(object sender, EventArgs e)
  2. {
  3. //decrement the curRecord
  4. curRecord--;
  5. if (curRecord < 0)
  6. curRecord = totRecord - 1;
  7. //call subroutine
  8. Navigate();
  9.  
  10.  
  11. }
And for the “Next” will simply increment the row number of Datatable by one.And here’s the code for “Next” Button.
  1. private void btnnext_Click(object sender, EventArgs e)
  2. {
  3. curRecord++;
  4. if (curRecord >= totRecord)
  5. curRecord = 0;
  6. Navigate();
  7. }
This time, you can test the application by pressing the “F5” or the “Start” button. And it should look like as shown below. d2 And here’s all the code used for this application.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10. namespace student_info
  11. {
  12. public partial class Form1 : Form
  13. {
  14. //declare new variable named dt as New Datatable
  15. DataTable dt = new DataTable();
  16. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  17. static string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + "/studentdb.mdb";
  18. OleDbConnection conn = new OleDbConnection(connection);
  19.  
  20. int curRecord = 0;
  21. int totRecord = 0;
  22.  
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. }
  27.  
  28. private void btnload_Click(object sender, EventArgs e)
  29. {
  30. //create a new datatable
  31. dt = new DataTable();
  32. //create our SQL SELECT statement
  33. string sql = "Select * from tblstudent";
  34. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  35. OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  36. //we fill the result to dt which declared above as datatable
  37. da.Fill(dt);
  38.  
  39. //set the curRecord to zero
  40. curRecord = 0;
  41. //get the total number of record available in the database and stored to totRecord Variable
  42. totRecord = dt.Rows.Count;
  43.  
  44.  
  45. //then we populate the datagridview by specifying the datasource equal to dt
  46. dataGridView1.DataSource = dt;
  47. }
  48.  
  49. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  50. {
  51. //it checks if the row index of the cell is greater than or equal to zero
  52. if (e.RowIndex >= 0)
  53. {
  54. //gets a collection that contains all the rows
  55. DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  56. //populate the textbox from specific value of the coordinates of column and row.
  57. txtid.Text = row.Cells[0].Value.ToString();
  58. txtfname.Text = row.Cells[1].Value.ToString();
  59. txtlname.Text = row.Cells[2].Value.ToString();
  60. txtcourse.Text = row.Cells[3].Value.ToString();
  61. txtgender.Text = row.Cells[4].Value.ToString();
  62. txtaddress.Text = row.Cells[5].Value.ToString();
  63.  
  64. }
  65.  
  66. }
  67.  
  68. private void Form1_Load(object sender, EventArgs e)
  69. {
  70.  
  71. }
  72.  
  73. private void Navigate()
  74. {
  75. txtid.Text = dt.Rows[curRecord][0].ToString();
  76. txtfname.Text = dt.Rows[curRecord][1].ToString();
  77. txtlname.Text = dt.Rows[curRecord][2].ToString();
  78. txtcourse.Text = dt.Rows[curRecord][3].ToString();
  79. txtgender.Text = dt.Rows[curRecord][4].ToString();
  80. txtaddress.Text = dt.Rows[curRecord][5].ToString();
  81. }
  82.  
  83. private void btnprev_Click(object sender, EventArgs e)
  84. {
  85. //decrement the curRecord
  86. curRecord--;
  87. if (curRecord < 0)
  88. curRecord = totRecord - 1;
  89. //call subroutine
  90. Navigate();
  91.  
  92.  
  93. }
  94.  
  95. private void btnnext_Click(object sender, EventArgs e)
  96. {
  97. curRecord++;
  98. if (curRecord >= totRecord)
  99. curRecord = 0;
  100. Navigate();
  101. }
  102.  
  103.  
  104.  
  105.  
  106. }
  107. }

Add new comment