DataRow
A DataRow
contains an individual row of data. The DataRow
type provides ways to add, remove, or read cells from the enclosing data structure.
How can you get a field from a DataRow
? You can use the Field extension. This eliminates the need to cast the object returned—it is a strongly-typed Function.
GetTable
functionIn these examples, the GetTable
function returns a fully formed DataTable
instance. It has four columns and five rows, for a total of 20 cells.
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
We see a simple way to construct a DataRow
instance using an Object array. The elements must be arranged to match the ordering of the DataRow
template itself.
DataTable
structure.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
We can get references to individual DataRows
from the DataTable
based on indexes. Then we can access cells from those rows using indexes or string
indexers.
DataRow
, and then look up a cell on those 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 ModuleShar Pei Yorkshire Terrier
For Each
How can you loop through the cells in a DataRow
? You can use the ItemArray
property. On the iteration variable in the For-Each
loop, we must test the type of the Object.
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 ModuleInteger String String DateTime
Remove
Sometimes you want to remove a row from an enclosing DataTable
, but not erase that row's data from memory. To do this, use the Remove()
Sub
.
DataTable
changes so that the second row is in the first position.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 ModuleFido
Delete
Delete
fully clears the memory inside the DataRow
instance you call it upon. In other words, this will yield an empty or null
row.
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 ModuleFido
The DataRow
provides a necessary level of abstraction for manipulating the DataTable
structure. The DataRow
is an essential type for developing data-driven VB.NET programs.