-3
如何在vb.net中使用for循环显示以下内容?分组使用循环
Type1
Desc1 1 $400
Desc2 1 $300
Type2
Desc3 1 $1400
Desc4 2 $2300
请帮忙。
如何在vb.net中使用for循环显示以下内容?分组使用循环
Type1
Desc1 1 $400
Desc2 1 $300
Type2
Desc3 1 $1400
Desc4 2 $2300
请帮忙。
没有进一步的信息,将很难制定一个确切的答案。但是,这个代码示例可能会解释如何解决您的问题。
只需创建一个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
感谢兰迪的所有帮助。 – 2012-07-13 16:06:14
请在您的问题中添加更多上下文,是否需要访问对象的这些字段? – 2012-07-12 20:08:35
我想我们需要更多的信息。你在循环什么(一些类的列表?)。应该包括一些你已经尝试过的代码。循环记录集值的 – 2012-07-12 20:09:44
。假设Type1下有2个descp,Type2下有3个descp。我想要一个通用的循环。 – 2012-07-12 20:11:06