2012-10-31 83 views
0

我有以下結構把自定義列表中的datagridview vb.net

matrix As Dictionary(Of String, Dictionary(Of Class1, Class2)) 

我願把這個結構的所有元素在這樣一個DataGridView:

Column1: String | Column2: Class1.name  | column3: class2.name 

任何人都有一個線索關於如何解決這個問題?我是新來datagridviews所以對如何開始添加行和colums(也許自動生成的列??)

回答

1

測試和工作樣本沒有線索:

Public Class Class1 
    Public name As String 
End Class 
Public Class Class2 
    Public name As String 
End Class 

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim matrix As New Dictionary(Of String, Dictionary(Of Class1, Class2)) 
    matrix.Add("hello", New Dictionary(Of Class1, Class2)) 
    matrix("hello").Add(New Class1 With {.name = "123"}, New Class2 With {.name = "321"}) 

    Dim dt As New DataTable 
    With dt.Columns 
    .Add("Column1") 
    .Add("Column2") 
    .Add("Column3") 
    End With 
    For i = 0 To matrix.Count - 1 
    Dim key As String = matrix.Keys(i) 
    Dim value As Dictionary(Of Class1, Class2) = matrix.Values(i) 
    For j = 0 To value.Count - 1 
     Dim class1Name As String = value.Keys(j).name 
     Dim class2Name As String = value.Values(j).name 
     Dim dr As DataRow = dt.NewRow 
     With dr 
     .Item("Column1") = key 
     .Item("Column2") = class1Name 
     .Item("Column3") = class2Name 
     End With 
     dt.Rows.Add(dr) 
    Next 
    Next 
    dataGridView1.DataSource = dt 
End Sub