需要对包含日期的ListView列进行排序 [英] Need to sort ListView columns that contain dates
问题描述
我需要对ListView的列进行排序。
有些列包含日期,其他列包含整数。
我做了一次以前在比较方法中我试过日期,如果那个
失败了我做了整数。
看起来有点不好 - 有更好的方法吗?
谢谢
公共函数比较(ByVal x As Object,ByVal y As Object)作为整数
实现IComparer.Compare
-snip
''''转换要与ListViewItem对象进行比较的对象。
lListviewX = CType(x,ListViewItem)
lListviewY = CType(y,ListViewItem)
尝试
''将作为参数传递的两个对象解析为DateTime。
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems (mColumnToSort)。文本)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubI TEMS(mColumnToSort)。文字)
''比较两个日期。
lCompareResult = DateTime.Compare(lFirstDate,lSecondDate)
>
''Catch
''如果两个比较对象都没有有效的日期格式比较两个项目
作为一个字符串
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text)' '或者可以这样做
结束尝试
I need to sort the columns of a ListView.
Some columns contain dates and others contain integers.
What I did once before is in the Compare method I tried date and if that
failed I did Integer.
Seems kinda not nice - is there a better way?
Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
-snip
''''Cast the objects to be compared to ListViewItem objects.
lListviewX = CType(x, ListViewItem)
lListviewY = CType(y, ListViewItem)
Try
''Parse the two objects passed as a parameter as a DateTime.
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems(mColumnToSort). Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)
''Compare the two dates.
lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)
'' Catch
''If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text) ''Or can do this
End Try
推荐答案
你不知道吗advance是哪列是每列的数据类型?如果没有,那么你必须猜测它,但我会在比较之前做到这一点,也就是说,第一个列表项目是否需要
,猜测列是否包含日期或整数然后
为每个案例调用不同的比较器。
-
祝你好运,
Carlos J. Quintero
MZ-Tools:Visual Studio .NET,VB6,VB5和VBA的生产力插件
您可以编码,设计和文档更快。
加载项开发人员的免费资源:
http://www.mztools.com
" ** **开发" < RE ************* @ a-znet.com> escribióenel mensaje
news:em ************* @ TK2MSFTNGP12.phx.gbl ...
Can′t you know in advance which is the data type of each column? If not then
you have to guess it, but I would do that before comparing, that is, take
the first listitem, guess if the column contains date or integer and then
call a different comparer for each case.
--
Best regards,
Carlos J. Quintero
MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
" **Developer**" <RE*************@a-znet.com> escribió en el mensaje
news:em*************@TK2MSFTNGP12.phx.gbl...
我需要排序ListView的列。
有些列包含日期,其他列包含整数。
我之前做的一次是在Compare方法中我试过日期,如果那样
失败了我做了整数。
看起来有点不好 - 有更好的方法吗?
感谢
公共函数比较(ByVal x As Object,ByVal y As Object )作为整数
实现IComparer.Compare
-snip
''''转换要与ListViewItem对象进行比较的对象。
> lListviewX = CType(x,ListViewItem)
lListviewY = CType(y,ListViewItem)
尝试
''解析传递的两个对象一个参数作为DateTime。
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems(mColumnToSort).Text)
Dim lSecondDate As System .DateTime =
DateTime.Parse(lLis tviewY.SubItems(mColumnToSort)。文字)
''比较两个日期。
lCompareResult = DateTime.Compare(lFirstDate,lSecondDate)
''抓住
''如果比较对象都没有有效的日期格式,则比较两个项目
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort) )。文本,
lListviewY.SubItems(mColumnToSort).Text)''或者可以做到这一点
结束尝试
I need to sort the columns of a ListView.
Some columns contain dates and others contain integers.
What I did once before is in the Compare method I tried date and if that
failed I did Integer.
Seems kinda not nice - is there a better way?
Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
-snip
''''Cast the objects to be compared to ListViewItem objects.
lListviewX = CType(x, ListViewItem)
lListviewY = CType(y, ListViewItem)
Try
''Parse the two objects passed as a parameter as a DateTime.
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems(mColumnToSort). Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)
''Compare the two dates.
lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)
'' Catch
''If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text) ''Or can do this
End Try
这是一个普遍使用的用户控件,所以除非我需要包含控件的表单来帮助你,否则很难。
我不能以某种方式获得方法中的类型吗?
谢谢
" Carlos J. Quintero [.NET MVP] " < CA ***** @ NOSPAMsogecable.com>写在
消息新闻:O
This is a generally used usercontrol so that would be difficult unless I
required the form containing the control to do something to help.
Can''t I get the type in the method somehow?
Thanks
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:O
************** @ TK2MSFTNGP12.phx.gbl ...
**************@TK2MSFTNGP12.phx.gbl...
你不能事先知道每列的数据类型是什么?如果不是
那么你必须猜测它,但我会在比较之前这样做,也就是说,取第一个listitem,猜测列是否包含日期或整数然后再调用另一个每个案例的比较。
-
致以最诚挚的问候,
Carlos J. Quintero
MZ-Tools:生产力插件适用于Visual Studio .NET,VB6,VB5和VBA
您可以更快地编写代码,设计和文档。
加载项开发人员的免费资源:
http://www.mztools.com
** **开发" < RE ************* @ a-znet.com> escribióenel mensaje
新闻:em ************* @ TK2MSFTNGP12.phx.gbl ...
Can′t you know in advance which is the data type of each column? If not
then you have to guess it, but I would do that before comparing, that is,
take the first listitem, guess if the column contains date or integer and
then call a different comparer for each case.
--
Best regards,
Carlos J. Quintero
MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
" **Developer**" <RE*************@a-znet.com> escribió en el mensaje
news:em*************@TK2MSFTNGP12.phx.gbl...
我需要对ListView的列进行排序。
有些列包含日期,其他列包含整数。
之前我曾做过的比较方法我尝试过日期,如果那个
失败我做了整数。
看起来有点不好 - 有更好的方法吗?
感谢
公共函数比较(ByVal x As Object,ByVal y As Object)As Integer <实现IComparer.Compare
-snip
''''转换要与ListViewItem对象进行比较的对象。
lListviewX = CType (x,ListViewItem)
lListviewY = CType(y,ListViewItem)
尝试
''将作为参数传递的两个对象解析为DateTime。
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems(mColumnToSort).Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListview Y.SubItems(mColumnToSort)。文字)
''比较两个日期。
lCompareResult = DateTime.Compare(lFirstDate,lSecondDate)
''抓住
''如果比较对象都没有有效的日期格式比较两个项目
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort) )。文本,
lListviewY.SubItems(mColumnToSort).Text)''或者可以做到这一点
结束尝试
I need to sort the columns of a ListView.
Some columns contain dates and others contain integers.
What I did once before is in the Compare method I tried date and if that
failed I did Integer.
Seems kinda not nice - is there a better way?
Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
-snip
''''Cast the objects to be compared to ListViewItem objects.
lListviewX = CType(x, ListViewItem)
lListviewY = CType(y, ListViewItem)
Try
''Parse the two objects passed as a parameter as a DateTime.
Dim lFirstDate As System.DateTime =
DateTime.Parse(lListviewX.SubItems(mColumnToSort). Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)
''Compare the two dates.
lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)
'' Catch
''If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text) ''Or can do this
End Try
这篇关于需要对包含日期的ListView列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!