VB.NET DataRow

The VB.NET programming language

A DataRow contains an individual row of data. It narrows the data abstraction to the level of the row. The DataRow type provides ways to add, remove, or read cells from the enclosing data structure.

DataTable

Example

One: 1

As we begin, please notice that the examples that follow invoke a function called GetTable. This function returns a fully formed DataTable instance.
It has four columns
and five rows,
for a total of 20 cells.

Tip:You can paste this function into the following examples to achieve compilation.

Program that creates DataTable instance: VB.NET

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

Create

Object keyword

We see a simple way to construct a DataRow instance using an Object array. When you create the Object array, please set it up so that the elements are arranged to match the ordering of the DataRow template itself.

Next:Invoke the Add method upon the Rows collection to turn the Object array into part of the enclosing DataTable structure.

Program that adds DataRow: VB.NET

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

Get

This section provides information

It is useful to acquire references to individual DataRow instances from the DataTable based on indexes. Once you acquire the DataRows, you can access cells from those rows using indexes or string indexers.

Here:In this example, we access the first and last row from the DataRow, and then look up a cell on those rows.

Program that accesses rows: VB.NET

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

Loop

Arrow indicates looping

How can you loop through the cells in a DataRow? You can use the ItemArray instance property upon the row instance. On the iteration variable in the For-Each loop, please notice that you must test the type of the Object.

And:After this, you could perform more specific operations based on that type.

Program that loops through row: VB.NET

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

Remove

Programming tip

Sometimes you want to remove a row from an enclosing DataTable, but not actually erase that row's data from memory. You can therefore remove a row and then add it somewhere else. To do this, use the Remove Sub.

Here:This example demonstrates that when you remove the first row, the DataTable changes so that the second row is in the first position.

Program that removes rows: VB.NET

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

Delete

Warning: exclamation mark

Delete actually clears the memory inside the DataRow instance you call it upon.
In other words,
this will yield an empty
or null row. And it will remove the row from the previously enclosing DataTable structure.

Nothing
Program that deletes rows: VB.NET

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

Field

Steps

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. It has a different syntax form.

DataRow Field

Summary

The DataRow provides a necessary level of abstraction for manipulating the DataTable structure. As an essential type for developing data-driven VB.NET programs, the DataRow is easy to use once the underlying concepts are understood.


VB.NET: Data