[英]How to Sort datagridview by column values which are of datetime type.?
按column2(我使用的列的名稱)對datagridview排序時出現問題
我在列中使用了日期時間類型值,現在我想使用以下命令對網格(名為conv_msg_grid)進行排序
conv_msg_grid.Sort(Column2, System.ComponentModel.ListSortDirection.Ascending)
但出現錯誤“對象必須為字符串類型”。
有什么問題嗎???
請幫幫我.....
似乎您的表中的數據類型不一致。 您的第一個項目是字符串類型,但其中一些是日期類型。 因此,當您嘗試排序並擊中不是字符串的內容時,就會發生此錯誤。
要解決它,您有兩個選擇。
在排序之前,將所有內容轉換為日期時間(或字符串)值。 (如果允許用戶添加日期,這是更好的選擇)
將數據插入DatagridView時,請確保所有值均為DateTime(或字符串)。 (如果用戶無法輸入日期,這是更好的選擇)
要執行(或字符串)選項,只需刪除convert.ToDateTime並在值上執行ToString。
Dim Column2 As DataGridViewColumn = DataGridView1.Columns(0)
For Each r As DataGridViewRow In DataGridView1.Rows
r.Cells(Column2.Index).Value = Convert.ToDateTime(r.Cells(Column2.Index).Value)
Next
conv_msg_grid.Sort(conv_msg_grid.columns(2),System.ComponentModel.ListSortDirection.Ascending)
卡倫切
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.