我知道,如何增加整数,字符串by for statement
但我想知道如何通过'for~next'语句更改名称。 例如,
Dim row_sort1 As DataTable = rows1.CopyToDataTable
Dim row_sort2 As DataTable = rows2.CopyToDataTable
Dim row_sort3 As DataTable = rows3.CopyToDataTable
Dim row_sort4 As DataTable = rows4.CopyToDataTable
Dim row_sort5 As DataTable = rows5.CopyToDataTable
Dim row_sort6 As DataTable = rows6.CopyToDataTable
Dim row_sort7 As DataTable = rows7.CopyToDataTable
我有这样的编码,糟糕的措辞所以我想通过'for~next'声明改变。 我想增加数据表名称的编号(1~7) 怎么能反映在这个编码中。我希望修复我的编码更简单实用。
我需要你的帮助 谢谢
答案 0 :(得分:1)
如果我理解你的问题是正确的,以下内容可能有所帮助。
'Note your datatables will be named Datatable0 to Datatable6
Dim DTs(6) As DataTable
For i = 0 To 6
DTs(i) = New DataTable
DTs(i).TableName = "Datatable" & i
Next
可能有更好的方法,但这会有效!
答案 1 :(得分:1)
我假设你有一个行集合,它是你要从中复制的主人。
我有类似OSKM的类似方法。更好地使用列表集合而不是数组。 要在集合中访问表格,您可以使用Linq。
' Given master rowcollection
Dim masterRow As EnumerableRowCollection(Of DataRow)
' Empty table collection
Dim tableList As New List(Of DataTable)
For t As Integer = 0 To 6
' copy Master to a new table
Dim newTable As DataTable = masterRow.CopyToDataTable()
' give the new table a name
newTable.TableName = "Table" & t.ToString
' Add new table to collection
tableList.Add(newTable)
Next
' Access a certain table (i.e. Table5) using Linq
Dim table5 As DataTable = tableList.FirstOrDefault(Function(x) x.TableName = "Table5")