vb.net 如何在 VB Visual Studio 2012 中按日期对 DataGridView 列进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25787834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:10:58  来源:igfitidea点击:

How to sort DataGridView Column by date in VB visual studio 2012?

vb.netdatagridview

提问by valzz

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. Could it sorted?

Column(1) 不是 DateTime 类型而是 SortText 类型,我必须设置这样的字符串。可以排序吗?

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: enter image description here

这是我的 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

结束班

回答by Bj?rn-Roger Kringsj?

There's a difference between storingand displayingdata.

storingdisplaying数据是有区别的。

You need to change you database table schema. The Tanggalcolumn should be of type dateor datetime. When you've fixed this, it's trivial to display dates using a custom format:

您需要更改数据库表架构。该Tanggal列的类型应为datedatetime。解决此问题后,使用自定义格式显示日期就很简单了:

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. There's an example at the bottom of this MSDN page.

如果由于某种原因无法更改架构,则需要通过实现IComparer创建自定义比较器。此 MSDN 页面底部有一个示例。

回答by user12220476

    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. Make sure X1 grid is Sort enabled Now simply click on Column 6 header to sort. Hope that works.

X1 是 DataGridView 列 1 将有您的日期前 5/4/1987 列 6 将计算为整数的 MS 日期数,并且必须转换为字符串。确保 X1 网格已启用排序 现在只需单击第 6 列标题即可进行排序。希望有效。