我花了大约一个小时来解决这个问题而无法找到解决方案。错误就行了: sw.Write(.Item(“Name”)。ToString&“,”)
Name字段是String数据类型。
If File.Exists(fileSavePath) Then
Using sw As StreamWriter = New StreamWriter(fileSavePath, False)
'Write Column Headers
sw.Write("Name" & "," & "Unit Number" & "," & "Sunday" & "," & "Monday" & "," & "Tuesday" & "," & "Wednesday" & "," & "Thursday" & "," & "Friday" & "," & "Saturday")
sw.WriteLine()
'Write Attendance Information
For i As Integer = 0 To ScoutDataSet.Tables("AllClasses").Rows.Count - 1
If ScoutDataSet.Tables("AllClasses").Rows(i).Item("UnitNumber").ToString = troopNumber Then
With ScoutDataSet.Tables("AllClasses").Rows
sw.Write(.Item("Name").ToString & ",") 'Write Name
sw.Write(.Item("UnitNumber").ToString & ",") 'Write Unit Number
sw.WriteLine() ' Go to next scout
End With
End If
Next
End Using
End If
答案 0 :(得分:0)
您的With
语句适用于ScoutDataSet.Tables("AllClasses").Rows
- 即行集合,因此该集合上的.Item
期望行索引。相反,您需要With ScoutDataSet.Tables("AllClasses").Rows(i)
:
'Write Attendance Information
For i As Integer = 0 To ScoutDataSet.Tables("AllClasses").Rows.Count - 1
If ScoutDataSet.Tables("AllClasses").Rows(i).Item("UnitNumber").ToString = troopNumber Then
With ScoutDataSet.Tables("AllClasses").Rows(i)
sw.Write(.Item("Name").ToString & ",") 'Write Name
sw.Write(.Item("UnitNumber").ToString & ",") 'Write Unit Number
sw.WriteLine() ' Go to next scout
End With
End If
Next