How to Create an Advance Record Navigation in C#

This tutorial is a continuation of our last topic called “How to Create a Simple Record Navigation in C#”. But at this moment, we will modify our code and add more additional control such as First Record, Last Record and No of Record. And this look like as shown below. d1 To start building with this application, let’s open first our last project called “student_info”. Then add Buttons and Label. Next, arrange and Design it the same as shown above figure. This time we will add functionality to our four buttons such as First, Previous, Next and Last. To start with, double click the “First record” button. Then add the following code:
  1. private void btnfirst_Click(object sender, EventArgs e)
  2.         {
  3.             //check if the current record is not equal to zero
  4.            if (curRecord != 0)
  5.                //set the current record to zero
  6.             curRecord = 0;
  7.             //call the Navaigate sub procedure
  8.             Navigate();
  9.             //set the record value
  10.             lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  11.              
  12.         }
Then for the “Previous Record” Button. Add the following code:
  1. private void btnprev_Click(object sender, EventArgs e)
  2.         {
  3.             //check if the current record is greater than zero
  4.             if (curRecord > 0)
  5.             {
  6.                 //then decrement the current record by one
  7.                 curRecord = curRecord - 1;
  8.  
  9.                 //call the Navaigate sub procedure
  10.                 Navigate();
  11.                 //set the record value
  12.                 lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  13.             }
  14.          
  15.         }
For the “Next Record” Button. Add the following code:
  1. private void btnnext_Click(object sender, EventArgs e)
  2.         {
  3.           //check if the current record is not equal to the total Record minus by one
  4.             if (curRecord != totRecord - 1)
  5.             {
  6.                 //increment the current record by one
  7.                 curRecord = curRecord + 1;
  8.                 //call the Navaigate sub procedure
  9.                 Navigate();
  10.                 //set the record value
  11.                 lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  12.             }
  13.         }
And lastly for “Last Record” Button. Here’s the following code:
  1. private void btnlast_Click(object sender, EventArgs e)
  2.         {
  3.             //check if the current Record is not Equal to the total record
  4.             if (curRecord != totRecord)
  5.                 //set the current record equal to total record
  6.                 curRecord = totRecord - 1;
  7.  
  8.             //call the Navaigate sub procedure
  9.             Navigate();
  10.             //set the record value
  11.             lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  12.         }
At this time, you now test the application by pressing the “F5” or the Start button. 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.  
  50.         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  51.         {
  52.             //it checks if the row index of the cell is greater than or equal to zero
  53.             if (e.RowIndex >= 0)
  54.             {
  55.                 //gets a collection that contains all the rows
  56.                 DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
  57.                 //populate the textbox from specific value of the coordinates of column and row.
  58.                 txtid.Text = row.Cells[0].Value.ToString();
  59.                 txtfname.Text = row.Cells[1].Value.ToString();
  60.                 txtlname.Text = row.Cells[2].Value.ToString();
  61.                 comboBox1.Text = row.Cells[3].Value.ToString();
  62.                 txtgender.Text = row.Cells[4].Value.ToString();
  63.                 txtaddress.Text = row.Cells[5].Value.ToString();
  64.                
  65.             }
  66.  
  67.         }
  68.  
  69.         private void Form1_Load(object sender, EventArgs e)
  70.         {
  71.             loadDatatoCombobox();
  72.         }
  73.         private void loadDatatoCombobox()
  74.         {
  75.              
  76.             //create a new datatable
  77.             DataTable table = new DataTable();
  78.             //create our SQL SELECT statement
  79.             string sql = "Select course from tblstudent";
  80.             try
  81.             {
  82.                 conn.Open();
  83.                 //then we execute the SQL statement against the Connection using OleDBDataAdapter
  84.                 OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
  85.                 //we fill the result to dt which declared above as datatable
  86.                 da.Fill(table);
  87.                 //we add new entry to our datatable manually
  88.                 //becuase Select Course is not Available in the Database
  89.                 table.Rows.Add("Select Course");
  90.                 //set the combobox datasource
  91.                 comboBox1.DataSource = table;
  92.                 //choose the specific field to display
  93.                 comboBox1.DisplayMember = "course";
  94.                 comboBox1.ValueMember = "course";
  95.                 //set default selected value
  96.                 comboBox1.SelectedValue = "Select Course";
  97.             }
  98.             catch (Exception ex)
  99.             {
  100.                 //this will display some error message if something
  101.                 //went wrong to our code above during execution
  102.                 MessageBox.Show(ex.ToString());
  103.             }
  104.  
  105.         }
  106.  
  107.         private void Navigate()
  108.         {
  109.             txtid.Text = dt.Rows[curRecord][0].ToString();
  110.             txtfname.Text = dt.Rows[curRecord][1].ToString();
  111.             txtlname.Text = dt.Rows[curRecord][2].ToString();
  112.             comboBox1.Text = dt.Rows[curRecord][3].ToString();
  113.             txtgender.Text = dt.Rows[curRecord][4].ToString();
  114.             txtaddress.Text = dt.Rows[curRecord][5].ToString();
  115.         }
  116.  
  117.         private void btnprev_Click(object sender, EventArgs e)
  118.         {
  119.             //check if the current record is greater than zero
  120.             if (curRecord > 0)
  121.             {
  122.                 //then decrement the current record by one
  123.                 curRecord = curRecord - 1;
  124.  
  125.                 //call the Navaigate sub procedure
  126.                 Navigate();
  127.                 //set the record value
  128.                 lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  129.             }
  130.          
  131.         }
  132.  
  133.         private void btnnext_Click(object sender, EventArgs e)
  134.         {
  135.           //check if the current record is not equal to the total Record minus by one
  136.             if (curRecord != totRecord - 1)
  137.             {
  138.                 //increment the current record by one
  139.                 curRecord = curRecord + 1;
  140.                 //call the Navaigate sub procedure
  141.                 Navigate();
  142.                 //set the record value
  143.                 lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  144.             }
  145.         }
  146.  
  147.         private void btnfirst_Click(object sender, EventArgs e)
  148.         {
  149.             //check if the current record is not equal to zero
  150.            if (curRecord != 0)
  151.                //set the current record to zero
  152.             curRecord = 0;
  153.             //call the Navaigate sub procedure
  154.             Navigate();
  155.             //set the record value
  156.             lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  157.              
  158.         }
  159.  
  160.         private void btnlast_Click(object sender, EventArgs e)
  161.         {
  162.             //check if the current Record is not Equal to the total record
  163.             if (curRecord != totRecord)
  164.                 //set the current record equal to total record
  165.                 curRecord = totRecord - 1;
  166.  
  167.             //call the Navaigate sub procedure
  168.             Navigate();
  169.             //set the record value
  170.             lblrecord.Text = curRecord + 1 + " of  " + totRecord;
  171.         }
  172.  
  173.  
  174.  
  175.        
  176.     }
  177. }

Add new comment