【问题标题】:StackOverflowException was unhandled in VB.NETStackOverflowException 在 VB.NET 中未处理
【发布时间】:2014-12-24 17:12:08
【问题描述】:

我想使用以下代码将行从DataGridViewX1 移动到另一个表单的DataGridView

按钮点击事件:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    frmEncodeDatabase.EncodingCompleteDataGridView.DataSource = DataGridViewX1.Rows
    frmEncodeDatabase.Show()
End Sub

另一个表单的DataGridViewRowsAdd 事件:

EncodingCompleteDataGridView.Rows.Add(frmEncode.DataGridViewX1.Rows)

现在上面的代码是我遇到问题的地方:StackOverflowException。 (错误描述如下图)

http://postimg.org/image/4d91ex49j/

【问题讨论】:

  • 您正在RowsAdded 事件中添加行。这将触发 RowsAdded 事件,该事件将添加新行并再次引发事件并添加新行并引发事件并添加行......
  • @Bjørn-Roger Kringsjå 那我应该把代码放在哪里?我应该将它添加到 UserAddRow 上吗?
  • 您应该始终操作底层数据源。在这种情况下,数据源绑定到第二个 DGV。
  • @Bjørn-Roger Kringsjå 我忘了注意我的第一个 DataGridView 不是数据有界的。我需要知道如何将第一个 DGV 的行传输到数据有界的那个 DataGridView(这会导致错误)。请帮帮我?
  • DataGridViewX1 的行中是否添加了代码或其他事件?您可以查看调用堆栈(DEBUG -> Windows - Call Stack)以更好地了解正在发生的事情。

标签: .net vb.net datagridview


【解决方案1】:

这是一个通用示例,可​​帮助您朝着正确的方向前进。

这个例子:

  • 演示如何将未数据绑定的 datagridview 中的行合并到数据绑定的 datagridview 中
  • 假设两个 datagridview 的列数和顺序相同。
  • 假设数据绑定的 gridview 绑定到数据表。

我意识到您的真实示例在单独的表单上具有数据网格视图,但对于此示例,代码更容易在一个表单上遵循。

要对此进行测试,请执行以下操作:

1)新建一个表单(Form1),添加2个datagridview(DataGridView1和DataGridView2)和一个按钮(Button1)

2) 将 Form1 上的代码替换为:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Manually add columns to the first datagridview which will not be data bound

        Me.DataGridView1.Columns.Add("Column1", "Column1")
        Me.DataGridView1.Columns.Add("Column2", "Column2")
        Me.DataGridView1.Columns.Add("Column3", "Column3")

        ' Poulate both datagridviews

        PopulateDataGrid1Manually()
        PopulateDataGrid2DataBound()
    End Sub

    Private Sub PopulateDataGrid1Manually()
        ' This datagridview has data added manually without being databound

        Dim sRow(Me.DataGridView1.ColumnCount - 1) As String

        For r As Int32 = 1 To 3 ' Adding 3 rows of test data
            For c As Int32 = 0 To Me.DataGridView1.ColumnCount - 1
                sRow(c) = "data" & r.ToString
            Next

            Me.DataGridView1.Rows.Add(sRow)
        Next
    End Sub

    Private Sub PopulateDataGrid2DataBound()
        ' This datagridview is databound to a datatable

        Dim dt As New DataTable

        dt.Columns.Add("Column1", GetType(String))
        dt.Columns.Add("Column2", GetType(String))
        dt.Columns.Add("Column3", GetType(String))

        Dim dr As DataRow

        For i As Int32 = 1 To 3 ' Adding 3 rows of test data
            dr = dt.NewRow
            dr("Column1") = "new" & i.ToString
            dr("Column2") = "new" & i.ToString
            dr("Column3") = "new" & i.ToString
            dt.Rows.Add(dr)
        Next

        Me.DataGridView2.DataSource = dt    ' Databind
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Copy rows from the first datagridview to the second datagridview that is data bound
        ' First copy the second datagridviews datasource into a new data table

        Dim dt As DataTable = CType(Me.DataGridView2.DataSource, DataTable).Copy
        Dim dr As DataRow

        ' Loop through all rows of the first datagridview and copy them into the data table

        For r As Int32 = 0 To Me.DataGridView1.Rows.Count - 1
            If Me.DataGridView1.Rows(r).IsNewRow = False Then   ' Do not copy the new row
                dr = dt.NewRow

                ' Loop through all cells of the first datagridview and populate the data row

                For c As Int32 = 0 To Me.DataGridView1.ColumnCount - 1
                    dr(c) = Me.DataGridView1.Rows(r).Cells(c).Value
                Next

                dt.Rows.Add(dr) ' Add the data row to the data table
            End If
        Next

        Me.DataGridView2.DataSource = dt    ' Rebind the second datagridview with the new table which has all the rows from both datagridviews
    End Sub
End Class

【讨论】:

  • 我在 Form1 中的 datagridview...嗯,已经有列并且没有数据源。它有10多个列。所以我要记住按钮点击。
  • 这只是一个示例,向您展示如何将行从一个 datagridview 添加到另一个。您的问题包括在 button_click 事件中显示来自另一个 datagridview 的数据源的代码。如果您不尝试通过单击按钮进行填充,您希望它如何准确运行?请编辑您的问题并提供更多详细信息,以便我或其他人可以更好地为您提供帮助。
  • 对于 button_click 我想要 DGV1 中的非数据绑定到数据绑定的 DGV2 的行。 DGV2 有一个数据源,而 DGV1 没有。您的 演示如何将未数据绑定的 datagridview 中的行组合到数据绑定的 datagridview 中 很好。现在我只想看看我得到的错误的截图:postimg.org/image/k65b52bxn
  • BindingSource的DataSource是什么类型的对象?您可以通过在给出错误的行上设置断点并将其键入到即时窗口中来查找:?Ctype(frmEncodeDatabase.EncodingCompleteDataGridView.DataSource, BindingSource).DataSource
  • Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable) DataGridView2.DataSource = dt.Copy() 它让我可以将内容从 dgv1 复制到 dgv2。
【解决方案2】:
Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable) 
DataGridView2.DataSource = dt.Copy() 

它可以让我将内容从 dgv1 复制到 dgv2。

【讨论】:

    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2012-11-24
    • 2011-02-17
    相关资源
    最近更新 更多