1

我在按 column2 排序 datagridview 时遇到问题(我使用的列的名称)

我在列中使用了日期时间类型值,现在我想使用以下命令对网格(命名为 conv_msg_grid)进行排序

conv_msg_grid.Sort(Column2, System.ComponentModel.ListSortDirection.Ascending)

但它给出了一个错误“对象必须是字符串类型。”

有什么问题???

请帮帮我......

4

2 回答 2

1

似乎您的表中有不一致的数据类型。您的第一项是字符串类型,但其中一些是日期类型。因此,当您尝试排序并遇到不是字符串的内容时,就会发生此错误。

要解决它,您有两个选择。

  1. 在排序之前将所有内容转换为日期时间(或字符串)值。(如果允许用户添加日期,这是更好的选择)

  2. 将数据插入 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
于 2013-03-11T07:02:02.003 回答
0

conv_msg_grid.Sort(conv_msg_grid.columns(2), System.ComponentModel.ListSortDirection.Ascending)

卡伦切

于 2013-07-24T08:36:06.233 回答