G
Guest
What I have is three forms. Form one with buttons 1, 2 and 3; and forms 2 and 3 with datagrids.
Button 1 populates arrays a(i,j) and b(i,j) with values.
Button 2 creates a datatable containing the a(i,j) array then adds that table to a dataset
which is in turn bound to a datagrid in an instance of form 2 called matrixa.
Button 3 creates a datatable containing the b(i,j) array then adds that table to a dataset
which is in turn bound to a datagrid in an instance of form 3 called matrixb.
When I start the debugger and click in sequence buttons 1 2 and 3, everything goes exactly as planned. If,
however I then press button 2 again I get an error: "Column 'Column1" does not belong to matrixa"
I have discovered that the problem dissapears if I have button 2 call populatea() twice.
What's going on here?
:
#Region "Public dimension statements"
Public a_m As Integer = 5
Public a_n As Integer = 5
Public a(6, 6) As Double
Dim matrix_A As New form2
Public b_m As Integer = 5
Public b_n As Integer = 5
Public b(6, 6) As Double
Dim matrix_B As New Form3
#End Region
#Region "populate arrays a and b"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim j As Integer
For i = 1 To 5
For j = 1 To 5
a(i, j) = i + j
b(i, j) = i * j
Next
Next
End Sub
#End Region
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
populateA()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
populateB()
End Sub
#Region "This is the 'A' data grid populate section"
Private Sub populateA()
Dim dta As New DataTable("matrixa")
Dim dca(a_n + 1) As DataColumn
Dim dra(a_m + 1) As DataRow
Dim dataset1 As New DataSet
'loop in columns
Dim i As Integer = 0
Dim j As Integer = 0
For j = 0 To a_n - 1
dca(j) = New DataColumn("")
dca(j).DataType = System.Type.GetType("System.Single")
dta.Columns.Add(dca(j))
Next
'create rows
For i = 0 To a_m - 1
dra(i) = dta.NewRow
For j = 0 To a_n - 1
dra(i)(j) = a(i + 1, j + 1)
Next j
Next i
'add rows to table
For i = 0 To a_m - 1
dta.Rows.Add(dra(i))
Next
'create instance of dataset
dataset1 = New DataSet
'use add method to enter table into dataset
dataset1.Tables.Add(dta)
'bind the dataset to the grid
matrix_A.Text = "MATRIX A"
matrix_A.MdiParent = Me
matrix_A.DataGrid1.SetDataBinding(dataset1, "matrixa")
matrix_A.Show()
dta = Nothing
dataset1 = Nothing
System.GC.Collect()
End Sub
#End Region
#Region "This is the 'B' data grid populate section"
Private Sub populateB()
Dim dtb As New DataTable("matrixb")
Dim dcb(b_n + 1) As DataColumn
Dim drb(b_m + 1) As DataRow
Dim dataset2 As New DataSet
'loop in columns
Dim i As Integer
Dim j As Integer
For j = 0 To b_n - 1
dcb(j) = New DataColumn
dcb(j).DataType = System.Type.GetType("System.Single")
dtb.Columns.Add(dcb(j))
Next
'create rows
For i = 0 To b_m - 1
drb(i) = dtb.NewRow
For j = 0 To b_n - 1
drb(i)(j) = b(i + 1, j + 1)
Next j
Next i
'add rows to table
For i = 0 To b_m - 1
dtb.Rows.Add(drb(i))
Next
'create instance of dataset
dataset2 = New DataSet
'use add method to enter table into dataset
dataset2.Tables.Add(dtb)
'bind the dataset to the grid
matrix_B.Text = "MATRIX B"
matrix_B.MdiParent = Me
matrix_B.DataGrid2.SetDataBinding(dataset2, "matrixb")
matrix_B.Show()
dtb = Nothing
dataset2 = Nothing
System.GC.Collect()
End Sub
#End Region
Button 1 populates arrays a(i,j) and b(i,j) with values.
Button 2 creates a datatable containing the a(i,j) array then adds that table to a dataset
which is in turn bound to a datagrid in an instance of form 2 called matrixa.
Button 3 creates a datatable containing the b(i,j) array then adds that table to a dataset
which is in turn bound to a datagrid in an instance of form 3 called matrixb.
When I start the debugger and click in sequence buttons 1 2 and 3, everything goes exactly as planned. If,
however I then press button 2 again I get an error: "Column 'Column1" does not belong to matrixa"
I have discovered that the problem dissapears if I have button 2 call populatea() twice.
What's going on here?
:
#Region "Public dimension statements"
Public a_m As Integer = 5
Public a_n As Integer = 5
Public a(6, 6) As Double
Dim matrix_A As New form2
Public b_m As Integer = 5
Public b_n As Integer = 5
Public b(6, 6) As Double
Dim matrix_B As New Form3
#End Region
#Region "populate arrays a and b"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim j As Integer
For i = 1 To 5
For j = 1 To 5
a(i, j) = i + j
b(i, j) = i * j
Next
Next
End Sub
#End Region
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
populateA()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
populateB()
End Sub
#Region "This is the 'A' data grid populate section"
Private Sub populateA()
Dim dta As New DataTable("matrixa")
Dim dca(a_n + 1) As DataColumn
Dim dra(a_m + 1) As DataRow
Dim dataset1 As New DataSet
'loop in columns
Dim i As Integer = 0
Dim j As Integer = 0
For j = 0 To a_n - 1
dca(j) = New DataColumn("")
dca(j).DataType = System.Type.GetType("System.Single")
dta.Columns.Add(dca(j))
Next
'create rows
For i = 0 To a_m - 1
dra(i) = dta.NewRow
For j = 0 To a_n - 1
dra(i)(j) = a(i + 1, j + 1)
Next j
Next i
'add rows to table
For i = 0 To a_m - 1
dta.Rows.Add(dra(i))
Next
'create instance of dataset
dataset1 = New DataSet
'use add method to enter table into dataset
dataset1.Tables.Add(dta)
'bind the dataset to the grid
matrix_A.Text = "MATRIX A"
matrix_A.MdiParent = Me
matrix_A.DataGrid1.SetDataBinding(dataset1, "matrixa")
matrix_A.Show()
dta = Nothing
dataset1 = Nothing
System.GC.Collect()
End Sub
#End Region
#Region "This is the 'B' data grid populate section"
Private Sub populateB()
Dim dtb As New DataTable("matrixb")
Dim dcb(b_n + 1) As DataColumn
Dim drb(b_m + 1) As DataRow
Dim dataset2 As New DataSet
'loop in columns
Dim i As Integer
Dim j As Integer
For j = 0 To b_n - 1
dcb(j) = New DataColumn
dcb(j).DataType = System.Type.GetType("System.Single")
dtb.Columns.Add(dcb(j))
Next
'create rows
For i = 0 To b_m - 1
drb(i) = dtb.NewRow
For j = 0 To b_n - 1
drb(i)(j) = b(i + 1, j + 1)
Next j
Next i
'add rows to table
For i = 0 To b_m - 1
dtb.Rows.Add(drb(i))
Next
'create instance of dataset
dataset2 = New DataSet
'use add method to enter table into dataset
dataset2.Tables.Add(dtb)
'bind the dataset to the grid
matrix_B.Text = "MATRIX B"
matrix_B.MdiParent = Me
matrix_B.DataGrid2.SetDataBinding(dataset2, "matrixb")
matrix_B.Show()
dtb = Nothing
dataset2 = Nothing
System.GC.Collect()
End Sub
#End Region