简体   繁体   English

循环遍历数据表中的特定行

[英]Loop through particular row in a DataTable

Following code gives values in the 8th column.以下代码给出了第 8 列中的值。 Following code is okay.下面的代码没问题。

Dim myDataTable As System.Data.DataTable = New System.Data.DataTable
For Each row As System.Data.DataRow In myDataTable.Rows
    MessageBox.Show(row.Item(7).ToString)
Next

Following code doesn't give values in the 8th row.以下代码未在第 8 行中给出值。 Following code is not okay.下面的代码不行。

Dim myDataTable As System.Data.DataTable = New System.Data.DataTable
For Each col As System.Data.DataColumn In myDataTable.Columns
    MessageBox.Show(col.Item(7).ToString)
Next

Any suggestion?有什么建议吗?

The DataColumn class doesn't have an Item property. DataColumn类没有Item属性。 If you want to iterate through the items of the 8th row, you can do so using the ItemArray property of the DataRow:如果要遍历第 8 行的项目,可以使用 DataRow 的ItemArray属性执行此操作:

For Each item In myDataTable.Rows(7).ItemArray
    MessageBox.Show(item.ToString)
Next
Dim dt As New DataTable()
Dim eightRow = dt.Rows(7)
For x = 0 To dt.Columns.Count - 1
    MessageBox.Show(eightRow(x).ToString())
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM