-3

如何在 vb.net 中使用 for 循环显示以下内容?

Type1
Desc1    1      $400
Desc2    1      $300

Type2
Desc3    1      $1400
Desc4    2      $2300

请帮忙。

4

1 回答 1

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
于 2012-07-12T22:03:19.323 回答