How to iterate all items in a given row in the DataTable

后端 未结 4 1366
眼角桃花
眼角桃花 2020-12-20 14:26

How to iterate all items in a given row in the DataTable . I have the following code to iterate all rows, I want another For loop to iterate all ce

相关标签:
4条回答
  • 2020-12-20 14:54

    your code is correct. you cn use this code...

    Dim tbl As New DataTable()
    Dim datarow As DataRow
    
    tbl.Columns.Add("column1")
    
    datarow = tbl.NewRow()
    datarow("column1") = "Data1"
    
    tbl.Rows.Add(datarow)
    For Each dr As DataRow In tbl.Rows
        For Each value In dr.ItemArray
           Label1.Text = value.ToString()
        Next
    Next
    
    0 讨论(0)
  • 2020-12-20 14:55

    You can use DataRow.ItemArray to do the job also

    the code would look something like this

    For Each item As var In Row.ItemArray
        //do something
    Next
    
    0 讨论(0)
  • 2020-12-20 14:58
    For Each row As DataRow In dt.Rows
        For Each column As DataColumn in dt.Columns
            Console.WriteLine(row(column))
        Next column
    Next row
    
    0 讨论(0)
  • 2020-12-20 15:16

    You have to loop through DataRow.ItemArray. In C#, we can do it by following code:

     foreach (DataRow dr in dt.Rows)
       {
         foreach (var item in dr.ItemArray)
           {
                    Console.WriteLine(item);
           }
       }
    

    This is equivalent to the following VB.NET code.

    For Each dr As DataRow In dt.Rows
        For Each item In dr.ItemArray
            Console.WriteLine(item)
        Next
    Next
    
    0 讨论(0)
提交回复
热议问题