我在按 column2 排序 datagridview 时遇到问题(我使用的列的名称)
我在列中使用了日期时间类型值,现在我想使用以下命令对网格(命名为 conv_msg_grid)进行排序
conv_msg_grid.Sort(Column2, System.ComponentModel.ListSortDirection.Ascending)
但它给出了一个错误“对象必须是字符串类型。”
有什么问题???
请帮帮我......
我在按 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)
卡伦切