vb.net 循环遍历特定数据表的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/613539/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:05:55  来源:igfitidea点击:

Loop through the rows of a particular DataTable

vb.netdatatable

提问by Angkor Wat

IDE : VS 2008, Platform : .NET 3.5,

IDE:VS 2008,平台:.NET 3.5,

Hi,

你好,

Here is my DataTable columns :

这是我的 DataTable 列:

ID Note Detail

身备注详情

I want to write sth like this :

我想这样写:

//below code block is not the right syntax


For each q in dtDataTable.Column("Detail")

    strDetail = Row of Column Detail

 Next

Can anyone give me a suggestion and show me a code sample please ? Thanks.

任何人都可以给我一个建议并向我展示一个代码示例吗?谢谢。

回答by Joel Coehoorn

For Each row As DataRow In dtDataTable.Rows
    strDetail = row.Item("Detail")
Next row

There's also a shorthand:

还有一个简写:

For Each row As DataRow In dtDataTable.Rows
    strDetail = row("Detail")
Next row

Note that Microsoft's style guidelines for .Net now specifically recommend against using hungarian type prefixes for variables. Instead of "strDetail", for example, you should just use "Detail".

请注意,Microsoft 的 .Net 样式指南现在特别建议不要对变量使用匈牙利语类型前缀。例如,您应该使用“Detail”而不是“strDetail”。

回答by jason

Dim row As DataRow
For Each row In dtDataTable.Rows
    Dim strDetail As String
    strDetail = row("Detail")
    Console.WriteLine("Processing Detail {0}", strDetail)
Next row

回答by BR1COP

Here's the best way I found:

这是我找到的最好方法:

    For Each row As DataRow In your_table.Rows
        For Each cell As String In row.ItemArray
            'do what you want!
        Next
    Next

回答by eglasius

You want to loop on the .Rows, and access the column for the row like q("column")

您想在 .Rows 上循环,并像 q("column") 一样访问该行的列

Just:

只是:

        For Each q In dtDataTable.Rows
            strDetail = q("Detail")
        Next

Also make sure to check msdndoc for any class you are using + use intellisense

还要确保检查您正在使用的任何课程的msdn文档 + 使用智能感知