[RESOLVED] [2005] General question about selecting rows from a DataTable
I am trying to improve my understanding of accessing (selecting) rows from a DataTable. Below are three methods that I have tried and all seam to return expected results. There are no bound controls to the DataTable.
My question is: Which method is the preferred method to select rows from a DataTable and why? Better efficiency, or more predictable, easier to code?
Thanks to all who contribute to this great forum.
Code:
EXAMPLE 1
str_SQL = "GameID Like " & "'" & str_CurentGameID & "'"
Dim rows As DataRow() = Me.ScoreboardDataSet.tbl_MatchData.Select(str_SQL)
For Each row As DataRow In rows
row("GameID") = str_NewGameID
Next
Code:
EXAMPLE 2
str_SQL = "GameID Like " & "'" & str_CurentGameID & "'"
Me.ScoreboardDataSet.tbl_MatchData.DefaultView.RowFilter = str_SQL
Dim row As DataRow
For Each view As DataRowView In Me.ScoreboardDataSet.tbl_MatchData.DefaultView
row = DirectCast(View.Row, DataRow)
row("GameID") = str_NewGameID
Next view
Code:
EXAMPLE 3
For Each row As ScoreboardDataSet.tbl_MatchDataRow In Me.ScoreboardDataSet.tbl_MatchData.Select(String.Format("GameID LIKE '{0}'", str_CurentGameID))
row.GameID = str_NewGameID
Next Mrow
Re: [2005] General question about selecting rows from a DataTable
I would generally only use the DefaultView.RowFilter in cases where I was binding, so example 2 is out.
Examples 1 and 3 are basically exactly the same except that example 1 is drawn out. Some would consider example 1 better for the sake of clarity, but I'd only go along with that for rank beginners. For anyone who has any reasonable understanding of VB example 3 is the way to go. It's concise: brief and clear.
Just note that you have lost some of your type safety in example 1. There's no need to declare the loop counter as type DataRow. It can be declared as type ScoreboardDataSet.tbl_MatchDataRow just like it is in example 3. You're enumerating exactly the same array in each case.
Re: [2005] General question about selecting rows from a DataTable
Quote:
Just note that you have lost some of your type safety in example 1...
Is the “Select(String.Format("GameID LIKE '{0}'", str_CurentGameID))” in Example 3 part of the added type safety” (over Example 1) or is the lost type safety in Example 1 only caused by declaring loop counter as type DataRow?
Re: [2005] General question about selecting rows from a DataTable
I'd always do this:
vb.net Code:
String.Format("GameID LIKE '{0}'", str_CurentGameID)
rather than this:
vb.net Code:
"GameID LIKE '" & str_CurentGameID & "'"
but that's not what I meant. I was actually talking about this:
vb.net Code:
row("GameID") = str_NewGameID
in place of this:
vb.net Code:
row.GameID = str_NewGameID
It's not a big deal but you created a typed DataSet for a reason so you may as well use it. By declaring the loop counter as your custom DataRow type you are able to use the second option.
Re: [2005] General question about selecting rows from a DataTable
Thank you... These are rules I can use.