简体   繁体   English

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

[英]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 and displaying data. storingdisplaying数据之间存在差异。

You need to change you database table schema.您需要更改数据库表架构。 The Tanggal column should be of type date or datetime . Tanggal列的类型应为datedatetime 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.

相关问题 将datagridview标题列导出到excel vb 2012 - export datagridview header column to excel vb 2012 如何使用vb.net对datagridview中的int列进行排序 - How to sort int column in datagridview using vb.net 如何保存日期和时间,然后在datagridview中将它们从新到旧排序,我正在使用Visual Studio 2015和SQL服务器 - How do I save date and time and then sort them from new to old in datagridview, I'm using Visual Studio 2015 and SQL server vb.net - 按数字对 datagridview 列进行排序 - vb.net - Sort datagridview column numerically 日期时间选择器 Visual Studio 2012 - Date Time Picker Visual Studio 2012 如何使用vb.net的自定义参数对datagridview列进行排序? - How do I sort a datagridview column with custom parameters for vb.net? 如何在包含DBNull值的DataGridView控件中对列进行排序? VB.NET - How do you sort a column in a DataGridView control that contains DBNull values? VB.NET 如何从VB .net插入/运行Windows命令(ping,getmac,vmic等)-Visual Studio 2012 - How to insert/run a Windows command (ping, getmac, vmic, etc) from VB .net - Visual Studio 2012 如何使用Windows Media Player在Visual Studio 2012 VB中播放列表框项目 - how to use windows media player to play listbox items in visual studio 2012 vb 如何从工具箱中添加表格并在 Visual Studio 2012 Express 中编写 vb.Net 代码 - How to add a table from toolbox and write vb.Net code in visual studio 2012 express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM