我有一个我正在解析的DataTable
,当我的值=" x"时,我试图找到给定列的列名。
我像这样循环DataTable
......
For Each row As DataRow In dt.Rows
For Each cell In row.ItemArray
columnCount = columnCount + 1
If cell.ToString() = "x" Then
Dim columnName As String = [THIS IS WHAT I WANT]
End If
Next
Next
当我以这种方式完成表格时,是否可以获取该列名称?
答案 0 :(得分:2)
您需要遍历row.ItemArray
中包含有关列的所有元数据的项目,而不是遍历dt.Columns
中仅包含值的项目。然后,您可以从行中访问该列的单元格值:
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
columnCount = columnCount + 1
Object cell = row(column)
If cell.ToString() = "x" Then
Dim columnName As String = column.ColumnName
End If
Next
Next
答案 1 :(得分:1)
这是一种方法:
Dim columnname As String = dt.Columns(columncount).ColumnName