0

下面是我尝试使用的一个循环,用于将 GridViewRow 中的值获取到 DataRow 对象中(使用 Visual Basic 的 Godforsaken 语言)。但是,在这一行:

                dr(i) = r.Cells(i).Text

我不断收到以下错误消息:

the value of type string cannot be converted to system.data.datarow

有人可以指出我如何做到这一点的正确方向吗?

        Dim rows As New List(Of GridViewRow)()

        For Each item As GridViewRow In grdExpProd.Rows
            rows.Add(item)
        Next

        Dim value As Integer = rows.Count

        Dim dt As New DataTable()


        For index As Integer = value - 1 To 0 Step -1
            Dim dr As DataRow()
            Dim r As GridViewRow = rows(index)

            For i As Integer = 0 To r.Cells.Count - 1
                dr(i) = r.Cells(i).Text
            Next

            dt.Rows.Add(dr)
        Next
4

3 回答 3

1

我相信您需要将文本添加到DataRows 项属性中,这将允许您访问 DataRow 的各个单元格。您还需要将 GridRowView 中存在的列添加到新的DataTable.

For i = 1 To rows(0).Cells.Count
    dt.Columns.Add("Header" & i)
Next

For index As Integer = value - 1 To 0 Step -1
    Dim dr As DataRow = dt.NewRow()
    Dim r As GridViewRow = rows(index)

    For i As Integer = 0 To r.Cells.Count 
        dr.Item(i) = r.Cells(i).Text
    Next

    dt.Rows.Add(dr)
Next
于 2012-09-09T02:02:47.157 回答
0

您不小心将 dr 创建为 DataRow 对象数组,而不是单个 DataRow 对象。尝试更换

Dim dr As DataRow()

Dim dr As New DataRow()
于 2012-09-09T00:35:44.517 回答
0

尝试更换

Dim dr As DataRow()

Dim dr As DataRow = dt.NewRow()

此外,正如另一位海报指出的那样,您需要在数据表中创建列,然后再尝试向其中添加行。

于 2012-09-09T00:52:50.640 回答