如何在vb.net中使用for循环显示以下内容?
Type1
Desc1 1 $400
Desc2 1 $300
Type2
Desc3 1 $1400
Desc4 2 $2300
请帮忙。
答案 0 :(得分:0)
如果没有进一步的信息,很难制定出精确的答案。但是,此代码示例可能会解释如何解决您的问题。
只需创建一个VB.Net控制台应用程序,将下面的代码示例复制并粘贴到主模块中即可运行。
Module Module1
Sub Main()
Dim table As New System.Data.DataTable("TestTable")
With table
.Columns.Add("Type", GetType(String))
.Columns.Add("Description", GetType(String))
.Columns.Add("Number", GetType(Integer))
.Columns.Add("Value", GetType(Decimal))
End With
table.Rows.Add("Type1", "Desc1", 1, 400.0)
table.Rows.Add("Type1", "Desc2", 1, 300.0)
table.Rows.Add("Type2", "Desc3", 1, 1400.0)
table.Rows.Add("Type2", "Desc4", 2, 2300.0)
table.Rows.Add("Type2", "Desc5", 2, 700.0)
Dim outputDictionary As New Dictionary(Of String, List(Of String))
For Each row As DataRow In table.Rows
Dim typeName As String = CStr(row("Type"))
If (Not outputDictionary.ContainsKey(typeName)) Then
outputDictionary.Add(typeName, New List(Of String))
End If
outputDictionary(typeName).Add(String.Format("{0} {1} {2}", CStr(row("Description")), CInt(row("Number")).ToString(), CStr(row("Value"))))
Next
For Each namedType In outputDictionary
Console.WriteLine(namedType.Key)
For Each item In namedType.Value
Console.WriteLine(" " & item)
Next
Next
Console.ReadKey(True)
End Sub
End Module