473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

datagrid binding problem

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

--
mark
Nov 20 '05 #1
8 1844
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
Nov 20 '05 #2
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
Nov 20 '05 #3
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
"Cor Ligthert" wrote:
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

Nov 20 '05 #4
Mark,

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

Cor
Nov 20 '05 #5

--
mark
"Cor Ligthert" wrote:
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.


Nov 20 '05 #6
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
Nov 20 '05 #7
Cor;

Yes your techn ique works well.

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


--
mark
"Cor Ligthert" wrote:
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

Nov 20 '05 #8
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/de...ourceTopic.asp

http://msdn.microsoft.com/library/de...classtopic.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
Cor;

Yes your techn ique works well.

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

--
mark
"Cor Ligthert" wrote:
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

Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order...
6
by: Alpha | last post by:
I have several textboxes that I need to chang the text when the selection row is changed in a datagrid. I have the following code. This textbox displayes the initial selection but when I click on...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
17
by: A_PK | last post by:
I have problem databinding the DataGrid with DataView/DataSet after the filter... I create the following proceudre in order for user to filter as many as they want, but the following code is only...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.