How to Sort Records in a ListView in Descending Order Using C#.

Submitted by: 
Language: 
Visitors have accessed this post 115 times.

If you want to display the latest transaction on the top of the ListView, this tutorial is just right for you. So, in this tutorial, I will teach you how to sort records in a ListView in descending order using C#. This method has the ability to retrieve the data in the database and display it into the ListView that will automatically sort the latest date in which the transaction had been made. In this way, you don’t have to scroll down the records in the ListView because it will place the latest record on the top of the ListView when the transaction is made.

Creating a Database

Create a database in the “PHPMyAdmin” and named it “dbtransaction”.
After that, execute the following query to create a table in the database that you have created.

  1.  
  2. CEATE TABLE `tbltransaction` (
  3. `TRANSID` int(11) NOT NULL,
  4. `ORNO` int(30) NOT NULL,
  5. `TRANSDATE` date NOT NULL,
  6. `AMOUNTSALE` double NOT NULL,
  7. `CASHIER` varchar(30) NOT NULL

Insert the data in the table.

  1.  
  2. INSERT INTO `tbltransaction` (`TRANSID`, `ORNO`, `TRANSDATE`, `AMOUNTSALE`, `CASHIER`) VALUES
  3. (1, 70004, '2019-02-21', 385, 'Janno Palacios'),
  4. (2, 70005, '2019-02-21', 385, 'Janno Palacios'),
  5. (3, 70002, '2019-02-17', 385, 'Janno Palacios'),
  6. (4, 70001, '2019-02-18', 385, 'Janno Palacios'),
  7. (5, 70006, '2019-02-19', 69, 'Janno Palacios'),
  8. (6, 70007, '2019-02-21', 69, 'Janno Palacios'),
  9. (7, 70003, '2019-02-07', 138, 'Janno Palacios');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#.
figure 1

Step 2

Add a ListView in the form. After that do the form just like shown below.
figure 2

Step 3

Add MySQL.Data.dll as references.

Step 4

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 5

Create a connection between c# and MySQL database and declare all the classes that are needed.

  1.  
  2. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbtransaction;sslMode=none");
  3. MySqlCommand cmd = new MySqlCommand();
  4. MySqlDataAdapter da = new MySqlDataAdapter();
  5. DataTable dt = new DataTable();
  6. string sql;

Step 6

Create a method for retrieving data in the database and display it in the ListView in descending order.

  1.  
  2. private void RetrieveDescending()
  3. {
  4. try
  5. {
  6. con.Open();
  7. cmd = new MySqlCommand();
  8. da = new MySqlDataAdapter();
  9. dt = new DataTable();
  10.  
  11. sql = "SELECT * FROM `tbltransaction` ORDER BY TRANSDATE DESC";
  12.  
  13. cmd.Connection = con;
  14. cmd.CommandText = sql;
  15.  
  16. da.SelectCommand = cmd;
  17. da.Fill(dt);
  18.  
  19. listView1.Items.Clear();
  20.  
  21. foreach (DataRow r in dt.Rows)
  22. {
  23. ListViewItem list = listView1.Items.Add(r.Field<int>(1).ToString());
  24. list.SubItems.Add(r.Field<DateTime>(2).ToString());
  25. list.SubItems.Add(r.Field<Double>(3).ToString());
  26. list.SubItems.Add(r.Field<string>(4));
  27. }
  28.  
  29.  
  30.  
  31. }
  32. catch (Exception ex)
  33. {
  34. MessageBox.Show(ex.Message);
  35. }
  36. finally
  37. {
  38. con.Close();
  39. da.Dispose();
  40. }
  41. }

Step 7

Write the following codes to display the data in the ListView in the first load of the form.

  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. RetrieveDescending();
  5. }

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.