Loop through the rows of a particular DataTable

前端 未结 4 1654
轮回少年
轮回少年 2021-02-01 03:06

IDE : VS 2008, Platform : .NET 3.5,

Hi,

Here is my DataTable columns :

ID Note Detail

I want to write sth like this :

//below cod         


        
相关标签:
4条回答
  • 2021-02-01 03:13

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

    Just:

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

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

    0 讨论(0)
  • 2021-02-01 03:16
    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".

    0 讨论(0)
  • 2021-02-01 03:24
    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
    
    0 讨论(0)
  • 2021-02-01 03:37

    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
    
    0 讨论(0)
提交回复
热议问题