Displaying DataGrid Rows

F

fripper

After getting some good help through this newsgroup I have finally figured
out one way to populate a DataGrid programmatically ... specifically by
using a Data Table, adding columns then rows to it and then binding it to
the DataGrid. Here is a simplified version of my code:

Dim DT as New DataTable
Dim DR as DataRow = DT.NewRow
DT.Columns.Add("Col. A")
DT.Columns.Add("Col. B")
For i = 1 to 3
Dim DRX as DataRow = DT.NewRow
DRX.Item(0) = i
DRX.Item(1) = 4 * i
DT.Rows.Add(DRX)
Next i
DataGridView1.DataSource = DT

When I run this code I only see the heading row [Col. A and Col. B] and one
detail row [with 1 in the first column and 4 in the second column]. I don't
understand why I don't see all three detail rows. Can someone explain this
and tell me where I am messing up?

Thanks very much.
 
J

james

You need to do this:
Dim i as Intger
in your dim statements and then it will work.
(just tried it to be sure)
james
 
C

Cor Ligthert

fripper,

I tried your code because I did not understand why it would not work. It
does work in my opinion as it should it gives a column and a columnheader
and 3 rows of data.

However it is good when you set in the top of your files Option Strict On
and Option Explicit On you will be pointed on program errors and late
bindings. Without that VBNet acts often as VB6 and is therefore a lot
slower.

(The second row is not necassery, however does not give the error)

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