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.
Field. 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 function. In these examples, the GetTable 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.
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. 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.
Next Invoke the Add method upon the Rows collection to turn the Object array into part of the enclosing 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
Get. 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.
Here In this example, we access the first and last row from the 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.
And After this, you could perform more specific operations based on that type.
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.
Here This example shows that when you remove the first row, the 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
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 14, 2022 (edit link).