【问题标题】:Argument 'Expression' cannot be converted to type 'DataGridViewRow'参数“表达式”无法转换为类型“DataGridViewRow”
【发布时间】:2013-06-25 13:35:58
【问题描述】:

我收到此错误:Argument 'Expression' cannot be converted to type 'DataGridViewRow'. 我不知道这意味着什么或如何解决它,它发生在这一行:

dt2.Rows(Val(selectedItem))("Position") = dt.Rows(selectedItem.Cells(1).Value)("Mouse Position")

谁能解释一下错误是什么以及如何解决它?

    Try

        If selectedItems Is Nothing Then
            For n = 0 To dt.Rows.Count - 1
                dt2.Rows.Add(n)
                dt2.Rows(n)("Position") = dt.Rows.Item(n)("Mouse Position")
            Next

        Else

            For Each selectedItem As DataGridViewRow In selectedItems


                dt2.Rows.Add(selectedItem)
                dt2.Rows(Val(selectedItem))("Position") = dt.Rows(selectedItem.Cells(1).Value)("Mouse Position")

            Next
        End If

    Catch ex As Exception
        MsgBox("Error", MsgBoxStyle.Exclamation, "Error!")
    End Try

【问题讨论】:

  • 堆栈跟踪是什么?
  • 您的意思是在(“鼠标位置”)末尾调用.Value
  • selectedItem 是一个 DataGridViewRow,对吧?什么应该返回表达式 Val(DataGridViewRow) ?
  • 我以为只是将该数据表的行和列设置为另一个数据表@Steve中第2列的选定行

标签: vb.net argumentexception


【解决方案1】:

我必须查看您之前的问题才能理解您的问题。
变量 dt2 是一个 DataTable,只有一个名为“Position”的列,因此向该 DataTable 的 DataRow 集合添加 DataGridViewRow 没有任何意义。

你的第一个循环应该是

For n = 0 To dt.Rows.Count - 1
    Dim r = dt2.NewRow();
    r("Position") = dt.Rows.Item(n)("Mouse Position")
    dt2.Rows.Add(r)
Next

while 第二个循环

For Each selectedItem As DataGridViewRow In selectedItems
    Dim r = dt2.NewRow()
    r("Position") = dt.Rows(selectedItem.Cells(1).Value.ToString)("Mouse Position")
    dt2.Rows.Add(r)
Next

【讨论】:

  • 现在它说Conversion from string to integer is not valid.
  • 在第二个循环中,您将获得单元格 (1) 的值,这可能是一个需要转换为整数的字符串。检查更新现在是否有效。
  • 更新后显示Input string is not in correct format
  • 你能告诉我 selectedItem 的 Cells(1) 的内容是什么吗?是有效号码吗?在上一个问题中,您曾经使用 selectedItem.Index 从 dt 数据表中提取值,为什么现在使用 Cells(1) 的内容?
  • 它的Cursor Position { x coord, y coord}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多