• We just launched and are currently in beta. Join us as we build and grow the community.

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

SaltLad

Shonen Battle Strategist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
120
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. CEATE TABLE

    `tbltransaction`

    (
  2. `TRANSID`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `ORNO`

    int

    (

    30

    )

    NOT

    NULL

    ,
  4. `TRANSDATE`

    date

    NOT

    NULL

    ,
  5. `AMOUNTSALE`

    double

    NOT

    NULL

    ,
  6. `CASHIER`

    varchar

    (

    30

    )

    NOT

    NULL


  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

Insert the data in the table.

  1. INSERT

    INTO

    `tbltransaction`

    (

    `TRANSID`

    ,

    `ORNO`

    ,

    `TRANSDATE`

    ,

    `AMOUNTSALE`

    ,

    `CASHIER`

    )

    VALUES


  2. (

    1

    ,

    70004

    ,

    '2019-02-21'

    ,

    385

    ,

    'Janno Palacios'

    )

    ,
  3. (

    2

    ,

    70005

    ,

    '2019-02-21'

    ,

    385

    ,

    'Janno Palacios'

    )

    ,
  4. (

    3

    ,

    70002

    ,

    '2019-02-17'

    ,

    385

    ,

    'Janno Palacios'

    )

    ,
  5. (

    4

    ,

    70001

    ,

    '2019-02-18'

    ,

    385

    ,

    'Janno Palacios'

    )

    ,
  6. (

    5

    ,

    70006

    ,

    '2019-02-19'

    ,

    69

    ,

    'Janno Palacios'

    )

    ,
  7. (

    6

    ,

    70007

    ,

    '2019-02-21'

    ,

    69

    ,

    'Janno Palacios'

    )

    ,
  8. (

    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#.
creating_application_c_12.png

Step 2

Add a ListView in the form. After that do the form just like shown below.
2019-09-01_3.png

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. using

    MySql.Data.MySqlClient

    ;

Step 5

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


  1. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=dbtransaction;sslMode=none"

    )

    ;
  2. MySqlCommand cmd =

    new

    MySqlCommand(

    )

    ;
  3. MySqlDataAdapter da =

    new

    MySqlDataAdapter(

    )

    ;
  4. DataTable dt =

    new

    DataTable(

    )

    ;
  5. string

    sql;

Step 6

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

  1. private

    void

    RetrieveDescending(

    )
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;
  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. sql =

    "SELECT * FROM `tbltransaction` ORDER BY TRANSDATE DESC"

    ;

  10. cmd.

    Connection

    =

    con;
  11. cmd.

    CommandText

    =

    sql;

  12. da.

    SelectCommand

    =

    cmd;
  13. da.

    Fill

    (

    dt)

    ;

  14. listView1.

    Items

    .

    Clear

    (

    )

    ;

  15. foreach

    (

    DataRow r in

    dt.

    Rows

    )
  16. {
  17. ListViewItem list =

    listView1.

    Items

    .

    Add

    (

    r.

    Field

    <

    int

    >

    (

    1

    )

    .

    ToString

    (

    )

    )

    ;
  18. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    DateTime>

    (

    2

    )

    .

    ToString

    (

    )

    )

    ;
  19. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    Double

    >

    (

    3

    )

    .

    ToString

    (

    )

    )

    ;
  20. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    string

    >

    (

    4

    )

    )

    ;
  21. }



  22. }
  23. catch

    (

    Exception ex)
  24. {
  25. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  26. }
  27. finally
  28. {
  29. con.

    Close

    (

    )

    ;
  30. da.

    Dispose

    (

    )

    ;
  31. }
  32. }

Step 7

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

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. RetrieveDescending(

    )

    ;
  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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,360

324,368

Top