Static DataTable Demo

This demo illustrates how to create a DataTable statically. This DataTable has two columns - Name and Age - and three rows.


NameAge
Scott25
Jisun24
Sam5


Source Code

<%@ Import Namespace="System.Data" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      'Create the DataTable
      Dim dt as New DataTable()
      
      'Create the columns
      Dim dcName as New DataColumn("Name", GetType(String))
      Dim dcAge as New DataColumn("Age", GetType(Integer))
      
      'Add the columns to the DataTable's Columns collection
      dt.Columns.Add(dcName)
      dt.Columns.Add(dcAge)
      
      'Add some rows
      Dim dr as DataRow
      dr = dt.NewRow()
      dr("Name") = "Scott"
      dr("Age") = 25
      dt.Rows.Add(dr)
      
      dr = dt.NewRow()
      dr("Name") = "Jisun"
      dr("Age") = 24
      dt.Rows.Add(dr)
      
      dr = dt.NewRow()
      dr("Name") = "Sam"
      dr("Age") = 5
      dt.Rows.Add(dr)
      
      
      'Bind the DataTable to the DataGrid
      dgPeople.DataSource = dt
      dgPeople.DataBind()
    End If      
  End Sub
</script>

<asp:DataGrid runat="server" id="dgPeople"
	HeaderStyle-BackColor="LightGray"
	HeaderStyle-Font-Bold="True" />


[Return to the article...]