Sorting Data Based on Date in VB.Net
Submitted by janobe on Monday, February 25, 2019 - 17:31.
In this tutorial, I will teach you how to sort data based on date in vb.net. In this method you can sort the data on the database based on the transaction date and it will then be displayed in the datagridview. It has two buttons that has functions such as descending and ascending order. It depends on you which button you will use in sorting data chronologically.
Creating Database
Create a database named “dbtransaction” After that, execute the following query to create a table and add data in the table- --
- -- Dumping data for table `tbltransaction`
- --
- (1, 70004, '2019-02-21', 385, 'Janno Palacios'),
- (2, 70005, '2019-02-21', 385, 'Janno Palacios'),
- (3, 70002, '2019-02-17', 385, 'Janno Palacios'),
- (4, 70001, '2019-02-18', 385, 'Janno Palacios'),
- (5, 70006, '2019-02-19', 69, 'Janno Palacios'),
- (6, 70007, '2019-02-21', 69, 'Janno Palacios'),
- (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 visual basic.
Step 2
Do the form just like shown below.
Step 3
Open the code editor and add a namespace to access MySQL libraries.- Imports MySql.Data.MySqlClient
Step 4
Create a connection between MSQL database and Visual Basic 2015. After that, declare all the classes that are needed.- Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=;database=dbtransaction;sslMode=none")
- Dim cmd As MySqlCommand
- Dim da As MySqlDataAdapter
- Dim dt As DataTable
- Dim sql As String
Step 5
Create a sub procedure for sorting in ascending.- Private Sub Sort_Ascending()
- Try
- con.Open()
- cmd = New MySqlCommand
- With cmd
- .Connection = con
- .CommandText = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` ORDER BY TRANSDATE ASC"
- End With
- da = New MySqlDataAdapter
- da.SelectCommand = cmd
- dt = New DataTable
- da.Fill(dt)
- DataGridView1.DataSource = dt
- Catch ex As Exception
- MsgBox(ex.Message)
- Finally
- con.Close()
- da.Dispose()
- End Try
- End Sub
Step 6
Create a sub procedure for sorting in descending.- Private Sub Sort_Descending()
- Try
- con.Open()
- cmd = New MySqlCommand
- With cmd
- .Connection = con
- .CommandText = "SELECT `ORNO`, `TRANSDATE` as 'Date', `AMOUNTSALE` as 'TOTALAMOUNT', `CASHIER` FROM `tbltransaction` ORDER BY TRANSDATE DESC"
- End With
- da = New MySqlDataAdapter
- da.SelectCommand = cmd
- dt = New DataTable
- da.Fill(dt)
- DataGridView1.DataSource = dt
- Catch ex As Exception
- MsgBox(ex.Message)
- Finally
- con.Close()
- da.Dispose()
- End Try
- End Sub
Step 7
Double click the “Sort by Ascending” button to fire theclick event handler
of it and call the method for ascending.
- Private Sub btn_ascending_Click(sender As Object, e As EventArgs) Handles btn_ascending.Click
- Sort_Ascending()
- End Sub
Step 8
Double click the “Sort by Descending” button to fire theclick event handler
of it and call the method for descending.
- Private Sub btn_descending_Click(sender As Object, e As EventArgs) Handles btn_descending.Click
- Sort_Descending()
- End Sub
Step 9
Press F5 to run your project. 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
- 1490 views