vb.net 按列名的数据行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22455957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
datarow by column name
提问by user1164545
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()
如何通过列名而不是 获得结果dr(i)(2)?因为如果我在前面的那个数据表中加一列,那么我得到的数据是错误的,我应该使用dr(i)(3). 所以我想在未来不改变源代码的情况下克服这个问题。就像是dr(i)("column_name").ToString()
回答by Neolisk
Exactly like you wrote it:
就像你写的那样:
dr(i)("column_name").ToString()
MSDN reference:
MSDN参考:
For strongly typed value of other types (for example, Integer, you could use Field(Of T)extension):
对于其他类型的强类型值(例如,整数,您可以使用Field(Of T)扩展名):
In your case,
在你的情况下,
dr(i).Field(Of String)("column_name")

