[英]How to sort DataGridView Column by date in VB visual studio 2012?
In my DGV, I have date list in the column (1):在我的 DGV 中,我在列 (1) 中有日期列表:
11-Sep-2014
11-May-2011
11-Jan-2014
11-Mar-2014
12-Sep-2010
how to get descending result like this:如何得到这样的降序结果:
11-Sep-2014
11-Mar-2014
11-Jan-2014
11-May-2011
12-Sep-2010
The Column(1) is not DateTime type but SortText type, I must set string like that. Column(1) 不是 DateTime 类型而是 SortText 类型,我必须设置这样的字符串。 Could it sorted?可以排序吗?
I have tried using code:我试过使用代码:
DGV.Columns(1).SortMode = DGV.Sort(DGV.Columns(1), System.ComponentModel.ListSortDirection.Descending)
but it's useless, it don't sort by date :(但它没用,它不按日期排序:(
this is my DGV:这是我的 DGV:
Okeh, this is my DGV code in brief: Okeh,这是我的 DGV 代码:
Imports System.Data.OleDb导入 System.Data.OleDb
Public Class LapTransaksiSimpanan公开课 LapTransaksiSimpanan
Public Sub Koneksi()
str = "provider=microsoft.jet.oledb.4.0;data source=dbkoperasi.mdb"
Conn = New OleDbConnection(str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
Sub TampilGrid()
da = New OleDbDataAdapter("select * from LapTransaksiSimpanan", Conn)
ds = New DataSet
da.Fill(ds, "LapTransaksiSimpanan")
DGV.DataSource = ds.Tables("LapTransaksiSimpanan")
'on the below I wanna to sort the column, my code below is useless :(
DGV.Sort(DGV.Columns(1), System.ComponentModel.ListSortDirection.Descending)
DGV.Columns("ID_Simpanan").Width = 120
DGV.Columns("NAK").Width = 37
DGV.Columns("Tanggal").Width = 75
DGV.Columns("Jumlah").Width = 110
End Sub
Private Sub Setoran_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Koneksi()
Call TampilGrid()
End Sub
End Class结束类
There's a difference between
storing
anddisplaying
data.storing
和displaying
数据之间存在差异。
You need to change you database table schema.您需要更改数据库表架构。 The Tanggal
column should be of type date
or datetime
. Tanggal
列的类型应为date
或datetime
。 When you've fixed this, it's trivial to display dates using a custom format:解决此问题后,使用自定义格式显示日期就很简单了:
Me.DGV.Columns("Tanggal").DefaultCellStyle.Format = "dd-MMM-yyyy"
If you for some reason cannot change the schema, then you need to create a custom comparer by implementing IComparer .如果由于某种原因无法更改架构,则需要通过实现IComparer创建自定义比较器。 There's an example at the bottom ofthis MSDN page . 此 MSDN 页面底部有一个示例。
Dim tnd As Date
For Me.i = 0 To X1.RowCount - 2
tnd = X1.Item(1, i).Value
X1.Item(6, i).Value = tnd.ToOADate.ToString
Next
X1 is DataGridView Column 1 would have your date ex 5/4/1987 Column 6 would be calculated as MS date number in integer and must be converted to string. X1 是 DataGridView 第 1 列将您的日期前 5/4/1987 第 6 列计算为整数的 MS 日期数,并且必须转换为字符串。 Make sure X1 grid is Sort enabled Now simply click on Column 6 header to sort.确保 X1 网格已启用排序 现在只需单击第 6 列标题即可进行排序。 Hope that works.希望有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.