是否可以对包含日期的dgv(未绑定)列进行排序,如果是这样的话? [英] Is it possible to sort a dgv (unbound) column containing dates, if so how?

查看:100
本文介绍了是否可以对包含日期的dgv(未绑定)列进行排序,如果是这样的话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过有关在DataGridView中对日期列进行排序的大量在线文章,但它们几乎都与这种或那种数据库相关联。其中一些人说,如果确保数据的类型为DateTime,则列将通过单击列标题的字形进行排序。这不是真的。我已将我的数据转换为DateTime类型,并通过使用IsDate()方法验证它。它仍然像数据是字符串或数字一样排序。我的解决方案,可能不是一个好的解决方案,是添加一个用于排序的列,使用ToFileTime()将日期转换为数值。这是因为ToFileTime()计算从1601年1月1日到你输入的日期的时间并返回当然正确排序的数值。



我读了很多有关在DGV中排序列的MSDN文章或教程,而不是其中一个我可以找到排序日期的文章或教程,它总是字符串或数字。



未绑定的DataGridView列可以真正对DateTime填充列进行排序......我不这么认为。但是,如果可以,怎么做?

I have read numerous on-line articles on sorting a date column in DataGridView, however they are nearly all tied to a database of one kind or another. A few of them say if you make sure the data is of type DateTime the column will sort by clicking on the glyph of the column header. This is not true. I have converted my data to DateTime type and verified that it is by using the IsDate() method. It still sorts as if the data were strings or numbers. My solution, probably not a good one, is to add a column for sorting with dates converted to numeric values using ToFileTime(). This works because the ToFileTime() calculates the time from Jan 1 1601 to the date you enter and returns the numeric value which of course sorts properly.

I have read many of the MSDN articles or tutorials on sorting columns in DGV and not one of them that I can find sort dates, it is always strings or numbers.

Can an unbound DataGridView column really sort a DateTime filled column...I don't think so. But, if it can, how?

推荐答案

当直接在DGV栏中输入文字时,你输入一个字符串。我的解决方案是从键盘输入字符串到列单元格,然后使用Date.Parse将其转换为日期类型,然后将其发送回相同的列单元格并使用DGV.Refresh()重新显示它。 Test Project使用一个按钮,Tab Stop属性设置为False。 DGV列格式属性设置为MM / dd / yyyy。 AllowUserToAddRows属性设置为False。该按钮添加一行,使DGV单元格可用于输入日期类型文本。按Enter键可进行转换,格式化和重新显示。现在单击列顶部的字形可以正确排序日期,可以是升序也可以是降序。测试项目接受许多类型的日期格式,例如年/月/日,月 - 日 - 年,月份拼写如7月等。它将所有不同的格式转换为MM / dd / yyyy。



这里是代码:



DGV日期栏目排序



公共类Form1

私有我作为整数



私有子btnAdd_Click(发件人作为对象,e作为EventArgs)处理btnAdd.Click

dgvData.Rows.Add ()

SendKeys.Send(vbTab)

SendKeys.Send(vbTab)

End Sub



Private Sub dgvData_CellEndEdit(sender As Object,e As DataGridViewCellEventArgs)处理dgvData.CellEndEdit

尝试

Dim myDate As Date = dgvData(0,i) .Value

dgvData(0,i).Value = Date.Parse(myDate)

dgvData.Refresh()

i = i + 1

Catch ex As Exception

MsgBox(你必须输入一个有效的日期)

返回

结束尝试

结束子

结束班
When entering text directly into a DGV column you are entering a string. My solution is to enter the string from the keyboard to the column cell then convert it to date type using Date.Parse and then send it back to the same column cell and redisplay it using DGV.Refresh(). The Test Project uses one button with the Tab Stop property set to False. The DGV column format property is set to MM/dd/yyyy. The AllowUserToAddRows property is set to False. The button adds a row making the DGV cell available to enter date type text. By pressing the Enter key the conversion, formatting and redisplay takes place. Now clicking the glyph at the top of the column sorts dates properly, either ascending or descending. The Test Project accepts many types of date formats, e.g. year/month/day, month-day-year, month spelled out like July etc. It converts all of the different formats to MM/dd/yyyy.

Here is the code:

DGV Date Column Sort

Public Class Form1
Private i As Integer

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
dgvData.Rows.Add()
SendKeys.Send(vbTab)
SendKeys.Send(vbTab)
End Sub

Private Sub dgvData_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvData.CellEndEdit
Try
Dim myDate As Date = dgvData(0, i).Value
dgvData(0, i).Value = Date.Parse(myDate)
dgvData.Refresh()
i = i + 1
Catch ex As Exception
MsgBox("You must enter a valid date")
Return
End Try
End Sub
End Class


这篇关于是否可以对包含日期的dgv(未绑定)列进行排序,如果是这样的话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆