datagrid binding problem

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
 
C

Cor Ligthert

Mark,

I do not see the reason, however when you send a problem to the newsgroup
than it has to be I think for this problem like this, however make it
readable for us.

When you say you do something with button 2 set than the procedure in
button2 and not in a Sub.

As well tell in what line the problem occurs, I assume in this one however
when that stays a question helping is much more difficult.
dta.Columns.Add(dca(j))

(Do you want to keep that array structure by the way, or are you using the
logic from the dataset, when you take the last as would be normal I would
not create those datacolumns/rows in a matrix before).

You are now creating ttree reference systems (datagrid, datatable, array of
items).

Cor
 
G

Guest

The matrix is funamental. The datagrid is for show. The datatable is a necessary go between. the line number is not identified.

You understand language barriers and I hope you can be more flexable with code presentation

You responce was appreciated.
mark
 
G

Guest

--
mark


Cor Ligthert said:
Mark,

Where did the error occurs, you did not write that?

Cor


I am not sure I understand your question.

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 believe this error I am seeing is a bug in VB.net - a serious one. It seems tedious I know, but could you try reporoducing the error by pasting my code into your developer. Use the separate populatea() function version I provided. This way you will be able to see how calling the function twice in a row eliminates the effect.
 
C

Cor Ligthert

Mark,

I assume that this is what you want to do
matrix_A.MdiParent = Me
matrix_A.DataGrid1.DataSource = Nothing
matrix_A.DataGrid1.DataSource = dataset1.Tables("matrixa")
matrix_A.Show()

And delete than those 3 instructions after that.
When you would want to clear the dataset that is
dataset.clear
calling the GC cost only extra processing time.

The Datagrid holds no data only references to it in the datasource, so when
you clear the dataset that one is empty.

(That databinding from the datagrid is the same by the way, however this is
more done).

I hope this helps?

Cor
 
G

Guest

Cor;

Yes your techn ique works well.

Is it possible to bind the datagrid directly to the array - bypassing the datatable altogether?
 
C

Cor Ligthert

Mark,

When I have understand this documentation well, it should go, however I
never tried that really because of what I write after the links.

http://msdn.microsoft.com/library/d...mWindowsFormsDataGridClassDataSourceTopic.asp

http://msdn.microsoft.com/library/d...tml/frlrfsystemcollectionsilistclasstopic.asp

Why not use the datatable as your datasource it is much more easier to
reference to than an array

dt.rows(0)(0) is the first item

dt.(rows(dt.rows.count-1)(dt.rows.itemlist.count-1) is the last one.

There are a lot of features in a datatable you do not have in an array.

(And before I forget I think it is better to set allowsort to false in your
datagrid).

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top