从另一个DGV中的数据创建新的DGV

时间:2014-07-24 13:08:02

标签: vb.net csv datagridview

我希望我能在这里找到一些有关我的问题的帮助! 我正在vb.net中开发一个小应用程序,它将CSV文件上传到datagridview,然后在单击按钮时,使用原始数据创建一个新的datagridview,其中包含一些单元格数据,以及来自第一个datagridview的几个不同单元格的串联。我有这个示例代码,如果CSV文件中只有一行,它会执行它应该执行的操作。一旦有超过1行,它会在"的第一行抛出此错误。 statment:

"未处理的类型' System.ArgumentOutOfRangeException'发生在mscorlib.dll

其他信息:指数超出范围。必须是非负数且小于集合的大小。"

以下是我正在使用的代码:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    DataGridView2.ColumnCount = 4
    DataGridView2.Columns(0).Name = "Order Number"
    DataGridView2.Columns(1).Name = "Name"
    DataGridView2.Columns(2).Name = "Address"
    DataGridView2.Columns(3).Name = "CityStateZip"

    For n As Integer = 0 To DataGridView1.Rows.Count - 2

        DataGridView2.Rows(n).Cells(0).Value = DataGridView1.Rows(n).Cells(0).Value
        DataGridView2.Rows(n).Cells(1).Value = DataGridView1.Rows(n).Cells(3).Value & ", " & DataGridView1.Rows(n).Cells(1).Value & " " & DataGridView1.Rows(n).Cells(2).Value
        DataGridView2.Rows(n).Cells(2).Value = DataGridView1.Rows(n).Cells(4).Value
        DataGridView2.Rows(n).Cells(3).Value = DataGridView1.Rows(n).Cells(5).Value & ", " & DataGridView1.Rows(n).Cells(6).Value & " " & DataGridView1.Rows(n).Cells(7).Value
    Next
End Sub

我也尝试过:

For Each row As DataGridViewRow In DataGridView1.Rows
MessageBox.Show(CStr(row.Cells(0).Value))
Next row

点击它会遍历每一行,并弹出一个msgbox,其中包含每个值。

那么有人能指出我正确的方向让我的按钮点击使用原始dgv中的一些数据创建一个新的datagridview吗?在创建新的dgv之后,我将使用新文件名将其导出回csv。

对不起,我自学了一些vb.net的基础知识,但我希望向你们学习。

提前致谢!

编辑: 以下是仍然存在问题的更新代码:

For Each r As DataGridViewRow In DataGridView1.Rows

        DataGridView2.Rows(r).Cells(0).Value = DataGridView1.Rows(r).Cells(0).Value
        DataGridView2.Rows(r).Cells(1).Value = DataGridView1.Rows(r).Cells(3).Value & ", " & DataGridView1.Rows(r).Cells(1).Value & " " & DataGridView1.Rows(r).Cells(2).Value
        DataGridView2.Rows(r).Cells(2).Value = DataGridView1.Rows(r).Cells(4).Value
        DataGridView2.Rows(r).Cells(3).Value = DataGridView1.Rows(r).Cells(5).Value & ", " & DataGridView1.Rows(r).Cells(6).Value & " " & DataGridView1.Rows(r).Cells(7).Value

    Next

但是它仍然会抛出错误"错误1类型&System; Windows.Windows.Forms.DataGridViewRow'无法转换为'整数'。"每次"行(r)"被称为。

解决: 好的,我终于明白了。下面是代码:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
DataGridView2.ColumnCount = 4
DataGridView2.Columns(0).Name = "Order Number"
DataGridView2.Columns(1).Name = "Name"
DataGridView2.Columns(2).Name = "Address"
DataGridView2.Columns(3).Name = "CityStateZip"

    For n As Integer = 0 To DataGridView1.Rows.Count - 2

        DataGridView2.Rows.Add()
        DataGridView2.Rows(n).Cells(0).Value = DataGridView1.Rows(n).Cells(0).Value
        DataGridView2.Rows(n).Cells(1).Value = DataGridView1.Rows(n).Cells(3).Value & ", " & DataGridView1.Rows(n).Cells(1).Value & " " & DataGridView1.Rows(n).Cells(2).Value
        DataGridView2.Rows(n).Cells(2).Value = DataGridView1.Rows(n).Cells(4).Value
        DataGridView2.Rows(n).Cells(3).Value = DataGridView1.Rows(n).Cells(5).Value & ", " & DataGridView1.Rows(n).Cells(6).Value & " " & DataGridView1.Rows(n).Cells(7).Value
    Next
End Sub

0 个答案:

没有答案