2007-08-03

Gets Array of all DataRow

After you create or bind data into DataSet and you need select some data from them. You can use select method in datable.

DataTable.Select Method (String) Gets an array of all DataRow objects that match the filter criteria in order of primary key (or lacking one, order of addition.)

Sample 1. You can copy code from Step to Create Dataset topic and try to run this sample.

Dim drSelect As DataRow() = ds.Tables(0).Select("Column1=3")
Dim sTemp As String = ""

If drSelect.Length > 0 Then
For Each drItem As DataRow In drSelect
sTemp = sTemp & drItem("Column2").ToString() & ","
Next

MessageBox.Show(sTemp.Substring(0, sTemp.Length - 1))
End If


Sample 2.

Private Sub GetRowsByFilter()
Dim t As DataTable
t = DataSet1.Tables("Orders")
' Presuming the DataTable has a column named Date.
Dim strExpr As String
strExpr = "Date > '1/1/00'"
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = t.Select(strExpr)
Dim i As Integer
' Print column 0 of each returned row.
For i = 0 to foundRows.GetUpperBound(0)
Console.WriteLine(foundRows(i)(0))
Next i
End Sub

1 comment:

Wicked Web Programmers said...

Good points. You can also initialize the array with pairs of keys and values.