C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: You can paste this function into the following examples to achieve compilation.
VB.NET program that creates DataTable instance
Module Module1
Function GetTable() As DataTable
' Generate a new DataTable.
' ... Add columns.
Dim table As DataTable = New DataTable
table.Columns.Add("Weight", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Breed", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
' ... Add rows.
table.Rows.Add(57, "Koko", "Shar Pei", DateTime.Now())
table.Rows.Add(130, "Fido", "Bullmastiff", DateTime.Now())
table.Rows.Add(92, "Alex", "Anatolian Shepherd Dog", DateTime.Now())
table.Rows.Add(25, "Charles", "Cavalier Kind Charles Spaniel", DateTime.Now())
table.Rows.Add(7, "Candy", "Yorkshire Terrier", DateTime.Now())
Return table
End Function
Sub Main()
' Acquire the DataTable instance.
Dim table As DataTable = GetTable()
End Sub
End Module
Next: Invoke the Add method upon the Rows collection to turn the Object array into part of the enclosing DataTable structure.
VB.NET program that adds DataRow
Module Module1
Sub Main()
Dim table As DataTable = GetTable()
' Create an array of four objects and add it as a row.
Dim v(3) As Object
v(0) = 7
v(1) = "Candy"
v(2) = "Yorkshire Terrier"
v(3) = DateTime.Now()
table.Rows.Add(v)
End Sub
End Module
Here: In this example, we access the first and last row from the DataRow, and then look up a cell on those rows.
VB.NET program that accesses rows
Module Module1
Sub Main()
Dim table As DataTable = GetTable()
' First row.
Dim row1 As DataRow = table.Rows(0)
Console.WriteLine(row1("Breed"))
' Last row.
Dim row2 As DataRow = table.Rows(table.Rows.Count - 1)
Console.WriteLine(row2("Breed"))
End Sub
End Module
Output
Shar Pei
Yorkshire Terrier
And: After this, you could perform more specific operations based on that type.
VB.NET program that loops through row
Module Module1
Sub Main()
Dim table As DataTable = GetTable()
' Get first row.
Dim row1 As DataRow = table.Rows(0)
' Loop over ItemArray.
For Each item As Object In row1.ItemArray
' Test the type of each element.
If (TypeOf item Is Integer) Then
Console.WriteLine("Integer")
ElseIf (TypeOf item Is String) Then
Console.WriteLine("String")
ElseIf (TypeOf item Is DateTime) Then
Console.WriteLine("DateTime")
End If
Next
End Sub
End Module
Output
Integer
String
String
DateTime
Here: This example demonstrates that when you remove the first row, the DataTable changes so that the second row is in the first position.
VB.NET program that removes rows
Module Module1
Sub Main()
' Get the DataTable.
Dim table As DataTable = GetTable()
' Get the first row.
Dim row As DataRow = table.Rows(0)
table.Rows.Remove(row)
' Get the new first row.
row = table.Rows(0)
Console.WriteLine(row("Name"))
End Sub
End Module
Output
Fido
VB.NET program that deletes rows
Module Module1
Sub Main()
' Get the DataTable.
Dim table As DataTable = GetTable()
' Get the first row and delete it.
Dim row As DataRow = table.Rows(0)
row.Delete()
' Get the new first row.
row = table.Rows(0)
Console.WriteLine(row("Name"))
End Sub
End Module
Output
Fido