Datagridview显示日期或时间(VB.NET) [英] Datagridview show date or time (VB.NET)

查看:153
本文介绍了Datagridview显示日期或时间(VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中一些列最初被指定为String,它们包含日期或时间,这取决于一些商务逻辑。



我正在更改列的类型(到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屋!

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