Domanda

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()

È stato utile?

Soluzione

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")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top