Imports System.Data
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' --- Sample to demonstrate how to create a table in memory
' --- Create an empty form with a datagridview, and a button.
' This code is the handler for the button_click
' The datagridview does not need to have any property's set - we will
' fill it and adjust a column layout from code
' --- Create the data table
Dim temptable As New DataTable
' --- Add a unique integer column to use as unique key
temptable.Columns.Add("id", Type.GetType("System.Int32"))
temptable.Columns("id").Unique = True
' --- Add some other data type test columns
temptable.Columns.Add("stringfield", Type.GetType("System.String"))
temptable.Columns.Add("numberfield", Type.GetType("System.Decimal"))
temptable.Columns.Add("boolfield", Type.GetType("System.Boolean"))
temptable.Columns.Add("datefield", Type.GetType("System.DateTime"))
For f = 1 To 10
' --- Define a new record for the table
Dim d_row As DataRow = temptable.NewRow
d_row("id") = f
d_row("numberfield") = (1.23 * f) / 2
d_row("stringfield") = "sometext" & Str(f)
d_row("boolfield") = True
d_row("datefield") = DateTime.Now
' --- Add it to the table
temptable.Rows.Add(d_row)
Next
' --- Bind the data table for viewing to a vanilla datagrid on the form
DataGridView1.DataSource = temptable
' --- Set our numeric column to always display as currency
DataGridView1.Columns("numberfield").DefaultCellStyle.Format = "c"
MsgBox("ok")
End Sub
End Class
Creating a table in memory with typed fields
This code sample demonstrates how to create and fill a table object in memory, and then bind it to a datagridview for display