Reputation: 139
I am using this code:
Dim dr() As DataRow = datatable.Select("id='" & st)
For i = 0 To dr.GetUpperBound(0)
result = dr(i)(2).ToString()
Next i
How do I get result by column name instead of dr(i)(2)
? Because if I add a column to that data table in front, then I get the wrong data, I should use dr(i)(3)
. So I want to overcome this without changing source code in the future. Something like dr(i)("column_name").ToString()
Upvotes: 3
Views: 20262
Reputation: 26424
Exactly like you wrote it:
dr(i)("column_name").ToString()
MSDN reference:
For strongly typed value of other types (for example, Integer, you could use Field(Of T)
extension):
In your case,
dr(i).Field(Of String)("column_name")
Upvotes: 10