Datagridview显示日期或时间(VB.NET) [英] Datagridview show date or time (VB.NET)
问题描述
我正在更改列的类型(到datetime)以允许正确的排序。我想知道是否可以根据行显示日期值或时间值。或者我将永远都要显示两者。
谢谢。
您可以处理 CellFormatting
事件。每当一个单元格被画出时,这个事件被触发。您可以更改正在显示的值,而无需更改单元格的基础值和格式。
通过这样做,您可以根据行和列
Private Sub CellFormatting_EventHandler(sender As Object,e As DataGridViewCellFormattingEventArgs)
如果e.ColumnIndex = 2 AndAlso CType(sender, DataGridView)(5,e.RowIndex).Value.ToString =SteveThen'或任何您喜欢的逻辑
e.Value = CType(e.Value,Date).ToShortTimeString
End If
End Sub
I have a datagridview in which some columns where initially specified as String and they contain date or time, depending on some bussiness logic.
I am changing the type of the column (to datetime) to allow correct sorting. And I would like to know if it is possible to show date values or time values depending on the row. Or I will always have to show both.
Thank you.
You can handle the CellFormatting
event. This event is fired everytime a cell is painted. You can change what value is being displayed without changing the underlying value and the format of the cell.
By doing this you can change the format depending on the row and column
Private Sub CellFormatting_EventHandler(sender As Object, e As DataGridViewCellFormattingEventArgs)
If e.ColumnIndex = 2 AndAlso CType(sender, DataGridView)(5, e.RowIndex).Value.ToString = "Steve" Then 'Or whatever logic you like
e.Value = CType(e.Value, Date).ToShortTimeString
End If
End Sub
这篇关于Datagridview显示日期或时间(VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!