How do I create a grid with 5 cols & 3 rows?

Jason.Jordan

Member
Joined
Dec 4, 2007
Messages
8
Programming Experience
10+
How do I create a grid with 5 cols & 3 rows?

ie: "a1","a2","a3","a4","a5"
"b1","b2","b3","b4","b5"
"c1","c2","c3","c4","c5"

The DataGrid seems to want a database/table...
can I not just...
DataGrid1.addrow "a1" & vbTab & "a2" & vbTab & "a3" & vbTab & "a4" & vbTab & "a5"

...Help! :(
 
Assuming you mean DataGridView, try :-

VB.NET:
        Dim arNewRow() As String = {"a1", "a2", "a3", "a4", "a5"}
        DataGridView1.Rows.Add(arNewRow)

or a long-winded version, but allows you to be slightly more specific

VB.NET:
        With DataGridView1
            .Rows.Add("a1")
            Dim intRowToUse As Int16 = 1
            If .AllowUserToAddRows = True Then intRowToUse = 2
            .Rows(.Rows.Count - intRowToUse).Cells(1).Value = "a2"
            .Rows(.Rows.Count - intRowToUse).Cells(2).Value = "a3"
            .Rows(.Rows.Count - intRowToUse).Cells(3).Value = "a4"
            .Rows(.Rows.Count - intRowToUse).Cells(4).Value = "a5"
        End With
 
...my vb.net "windows CE 5.0 Device" project has NO DATAVIEW control :(

Thanks for the help InertiaM

and I think I see why I couldn't work out a way of manually adding to a grid...

...my vb.net "windows CE 5.0 Device" project
has NO DataGridView control

:(

...thats strange Micro$oft products are normally SO FANTASTIC :p
 
Only control available is a "DataGrid" :(

Looks like I'm going to have to move my project BACK to PocketPC2003
and use a ListBox with font set to Courier New - so the cols line up

VB.NET Windows CE 5.0 will not let me
use a ListBox with font set to Courier New...

...thats strange Micro$oft products are normally SO FANTASTIC :p
 
OK, try something like this :-

VB.NET:
        Dim dt As DataTable = New DataTable
        dt.Columns.Add("Col 1")
        dt.Columns.Add("Col 2")
        dt.Rows.Add("a1", "a2")
        DataGrid1.DataSource = dt
        dt.dispose()

I'm not sure it's technically correct, but it's worth a go.
 
Thanks again InertiaM
It's was worth a go...

however I got...
'Columns' is not a member of 'System.Windows.Forms.DataGrid'.

I think you are on the right track...
ie: creating a New DataTable on the fly
and wiring that into the DataGrid

...a steep learning curve/google-search-romp for a vb6-er like myself,
I'm almost tempted to write my own simple grid that just displays a 2d array
because vb.net compact looks like it hasn't got one...
:(
..thats strange Micro$oft products are normally SO FANTASTIC :p
 
'Columns' is not a member of 'System.Windows.Forms.DataGrid'.

I'm confused by that. Here's how I did it with a blank project (Pocket PC 2003), and it works :-

1. Add reference to System.Data
2. Add reference to System.XML
3. Place a Datagrid on the form called Datagrid1
4. Place a Button on the form called Button1
5. Code :-

VB.NET:
Imports System.Data

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = New DataTable
        dt.Columns.Add("Col 1")
        dt.Columns.Add("Col 2")
        dt.Rows.Add("a1", "a2")
        DataGrid1.DataSource = dt
        dt.dispose()
    End Sub
End Class
 
Last edited:
Yep that worked InertiaM
- many thanks

I hadn't added "reference to System.XML"

...if you're ever in Dublin Ireland its my twist for creamy guinness

again many thanks, is there any way I can add to your site rating,
(there should be a [flag InertiaM as a god] button)
 
Tried setting col width using GridColumnStyles

Tried setting col width using GridColumnStyles got...

"Specified argument was out of the range of valid values"
...on line...
TableStyle.GridColumnStyles.Item(0).Width = 20

with this code...
Dim dt As DataTable = New DataTable
dt.Columns.Add("Company")

'rows are read in from a txt file - but here for example I'll use acme ltd
dt.Rows.Add("acme ltd")

DataGrid1.DataSource = dt
dt.Dispose()

Dim TableStyle As DataGridTableStyle = New DataGridTableStyle()
TableStyle.GridColumnStyles.Item(0).Width = 20 '<<<ERR HERE!
DataGrid1.TableStyles.Add(TableStyle)

...I'm probably making a simple error! any ideas?
 
VB.NET:
Imports System.Data

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As DataTable = New DataTable
        dt.Columns.Add("Col 1")
        dt.Columns.Add("Col 2")
        dt.Rows.Add("a1", "a2")
        DataGrid1.DataSource = dt
        dt.Dispose()

        Dim tbStyle As DataGridTableStyle = New DataGridTableStyle
        DataGrid1.TableStyles.Add(tbStyle)

        Dim dgc As DataGridColumnStyle
        dgc = DataGrid1.TableStyles(0).GridColumnStyles(0)
        dgc.Width = 500
        dgc.Dispose()

        tbStyle.Dispose()

    End Sub
End Class

More Guinness please :D
 
"Programming Microsoft Visual Basic .NET" from Microsoft Press. ISBN 0-7356-1375-0 - oh, and it's 1576 pages long :eek: It used to be £43.

Mainly though, knowledge gained from this forum, searching the internet and trying things until they work! I've only changed to .NET in the last three weeks after years of VB6, 5 and 4 :)
 
Back
Top