The Simple Way to Navigate Records Based On ListView in C#

This time, I will teach you how to navigate records based on ListView in C# and MySQL Database. This method has the ability to navigate records in the ListView. It also provides a next and previous button that allows the movement of records back and forth. In this way, you can control the data to be displayed in the listview.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access MySQL libraries
  1.  
  2. using MySql.Data.MySqlClient;

Step 4

Establish a connection between C# and MySQL database. After that, declare all the classes and variables that are needed.
  1.  
  2.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none");
  3.         MySqlCommand cmd;
  4.         MySqlDataAdapter da;
  5.         DataTable dt;
  6.         String sql;
  7.         int maxrow;
  8.         int inc_value;
  9.         int resultPerPage = 10;
  10.         int startResult;
  11.  

Step 5

Create a method for retrieving single data in the database.
  1.  
  2.         private void loadSingleResult(String sql)
  3.         {
  4.             try
  5.             {
  6.                 con.Open();
  7.  
  8.                 cmd = new MySqlCommand();
  9.                 da = new MySqlDataAdapter();
  10.                 dt = new DataTable();
  11.  
  12.                 cmd.Connection = con;
  13.                 cmd.CommandText = sql;
  14.                 da.SelectCommand = cmd;
  15.                 da.Fill(dt);
  16.                 maxrow = dt.Rows.Count - 1;
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 MessageBox.Show(ex.Message);
  21.             }
  22.             finally
  23.             {
  24.                 con.Close();
  25.                 da.Dispose();
  26.             }
  27.  
  28.         }
  29.  

Step 6

Create a method for retrieving list of data in the database
  1.  
  2.         private void loadResultList(String sql, ListView lst)
  3.         {
  4.             try
  5.             {
  6.                 con.Open();
  7.  
  8.                 cmd = new MySqlCommand();
  9.                 da = new MySqlDataAdapter();
  10.                 dt = new DataTable();
  11.  
  12.                 cmd.Connection = con;
  13.                 cmd.CommandText = sql;
  14.                 da.SelectCommand = cmd;
  15.                 da.Fill(dt);
  16.  
  17.                 lst.Items.Clear();
  18.                 foreach (DataRow r in dt.Rows)
  19.                 {
  20.                     var list = lst.Items.Add(r.Field<string>(1).ToString());
  21.                     list.SubItems.Add(r.Field<string>(2).ToString());
  22.                     list.SubItems.Add(r.Field<int>(3).ToString());
  23.                     list.SubItems.Add(r.Field<string>(4).ToString());
  24.                 }
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 MessageBox.Show(ex.Message);
  29.             }
  30.             finally
  31.             {
  32.                 con.Close();
  33.                 da.Dispose();
  34.             }
  35.  
  36.         }

Step 7

Create a method for the next records.
  1.         private void next_record()
  2.         {
  3.  
  4.             if (inc_value != maxrow - 2)
  5.             {
  6.                 inc_value = inc_value + 1;
  7.                 startResult = inc_value * resultPerPage;
  8.             }
  9.             else
  10.             {
  11.                 if (inc_value == maxrow)
  12.                 {
  13.                     return;
  14.                 }
  15.             }
  16.             sql = "Select * from subject limit " + startResult + "," + resultPerPage + "";
  17.             loadResultList(sql, listView1);
  18.  
  19.         }

Step 8

Create a method for the previous records
  1.         private void previous_record()
  2.         {
  3.             if (inc_value == 0)
  4.             {
  5.                 return;
  6.             }
  7.             else if (inc_value > 0)
  8.             {
  9.                 inc_value = inc_value - 1;
  10.                 startResult = inc_value * resultPerPage;
  11.             }
  12.  
  13.             sql = "Select * from subject limit " + startResult + "," + resultPerPage + "";
  14.             loadResultList(sql, listView1);
  15.  
  16.         }

Step 9

Write the following codes for retrieving and displaying data in the first load of the form.
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             sql = "Select * from subject";
  4.             loadSingleResult(sql);
  5.  
  6.             sql = "Select * from subject limit 10";
  7.             loadResultList(sql, listView1);
  8.         }

Step 10

Write the following codes for the next button
  1.         private void btn_next_Click(object sender, EventArgs e)
  2.         {
  3.             next_record();
  4.         }

Step 11

Write the following codes for the previous button
  1.         private void btn_previous_Click(object sender, EventArgs e)
  2.         {
  3.             previous_record();
  4.         }
The complete source code is included you can download it and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment