【问题标题】:Loop through the rows of a particular DataTable循环遍历特定 DataTable 的行
【发布时间】:2010-10-11 10:37:37
【问题描述】:

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

嗨,

这是我的 DataTable 列:

身份证件详情

我想这样写:

//below code block is not the right syntax


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

    strDetail = Row of Column Detail

 Next

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

【问题讨论】:

    标签: vb.net datatable


    【解决方案1】:

    这是我找到的最好方法:

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

    【讨论】:

      【解决方案2】:

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

      只是:

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

      还要确保检查 msdn doc 以了解您正在使用的任何课程 + 使用智能感知

      【讨论】:

        【解决方案3】:
        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
        

        【讨论】:

          【解决方案4】:
          For Each row As DataRow In dtDataTable.Rows
              strDetail = row.Item("Detail")
          Next row
          

          还有一个速记:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-01
            • 1970-01-01
            • 2020-08-05
            相关资源
            最近更新 更多