Popular Posts

Saturday, March 21, 2009

Creating Data Table In VB.NET

Creating DataTable

Visual Basic allows us to create our own Tables and work with them. The DataTable is an in-memory representation of a block of data. We can create our own tables in code using a DataSet and the types defined in the System.Data.OleDb or System.Data.SqlClient namespaces. The following are the core properties that are used while creating a DataTable.

CaseSensitive: Indicates whether string comparisons in the table are case-sensitive or not.
ChildRelations: Returns the collection of child relations of the DataTable (if any).
Columns: Returns the collection of columns that belong to this table.
Constraints: Gets the constraints maintained by this table.
DataSet: Gets the dataset that contains this table.
DefaultView: Gets a customized view of the table that may include a filtered view or a cursor position.
MinimumCapacity: Gets/Sets the initial number of rows in the table.
ParentRelations: Gets the collection of parent relations for this table.
PrimaryKey: Gets/Sets a primary key for the table.
Rows: Returns the collection of rows that belong to this table.
TableName: Gets/Sets the name of the table.

Creating a DataTable in Code

Let's see how we can create a DataTable in code. Open a new form and drag a Button and a DataGrid from the toolbox. We will load the table which we create in code into the DataGrid once the Button is clicked. We will create a DataTable named "Customers" , with "Name", "Product" and "Location" as three columns in the table. We will create three rows and three columns for this table. The following code shows how to create a table and load the table into the DataGrid.

Public Class Form1 Inherits System.Windows.Forms.Form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim Table1 As DataTable
Table1 = New DataTable("Customers")
'creating a table named Customers
Dim Row1, Row2, Row3 As DataRow
'declaring three rows for the table
Try
Dim Name As DataColumn = New DataColumn("Name")
'declaring a column named Name
Name.DataType = System.Type.GetType("System.String")
'setting the datatype for the column
Table1.Columns.Add(Name)
'adding the column to table
Dim Product As DataColumn = New DataColumn("Product")
Product.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Product)
Dim Location As DataColumn = New DataColumn("Location")
Location.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Location)

Row1 = Table1.NewRow()
'declaring a new row
Row1.Item("Name") = "Reddy"
'filling the row with values. Item property is used to set the field value.
Row1.Item("Product") = "Notebook"
'filling the row with values. adding a product
Row1.Item("Location") = "Sydney"
'filling the row with values. adding a location
Table1.Rows.Add(Row1)
'adding the completed row to the table
Row2 = Table1.NewRow()
Row2.Item("Name") = "Bella"
Row2.Item("Product") = "Desktop"
Row2.Item("Location") = "Adelaide"
Table1.Rows.Add(Row2)
Row3 = Table1.NewRow()
Row3.Item("Name") = "Adam"
Row3.Item("Product") = "PDA"
Row3.Item("Location") = "Brisbane"
Table1.Rows.Add(Row3)
Catch
End Try

Dim ds As New DataSet()
ds = New DataSet()
'creating a dataset
ds.Tables.Add(Table1)
'adding the table to dataset
DataGrid1.SetDataBinding(ds, "Customers")
'binding the table to datagrid
End Sub

End Class

When you run the above code and click the Button, a table is created and the created table loads into the DataGrid. You can add any number of rows and columns to the table. I added only three for the purpose of explanation. Other DataType values supported are: System.Boolean, System.Byte, System.Char, System.DateTime, System.Decimal, System.Double, System.Int16, System.Int32, System.Int64, System.SByte, System.Single. You can use any one of the above said data types depending on the type of data you will have in the columns.

The image below displays output from above code.


No comments:

Post a Comment