VB.Net程序设计:Dictionary和Collection搭配使用

前端之家收集整理的这篇文章主要介绍了VB.Net程序设计:Dictionary和Collection搭配使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

只为自己练习,参考,备忘。或许对你没有参考价值,请不要模仿。

一个单据有几个小项目,每个小项目又有几个小项目。可以理解:一个主表,有子表,子表又有子表。

某些单据套用,简单的处理程序。

Public Class Form2            '一个总集合:子表。         
    Dim mList As New Dictionary(Of String,Collection)        '一个子表里面的子表记录集         
    Dim mcc As New Collection

    Private Sub ShowList()
        Me.ListView1.Items.Clear()
        If mList.Count > 0 Then
            Dim mc As Collection
            Dim item As ListViewItem
            Dim strName As String
            For Each obj As KeyValuePair(Of String,Collection) In mList
                mc = obj.Value
                strName = String.Empty
                item = New ListViewItem
                For Each c As Object In mc
                    strName &= CType(c,DataGridViewRow).Cells(1).Value.ToString
                Next
                item.Text = strName
                item.ImageIndex = 0
                item.Tag = mc.GetHashCode.ToString
                Me.ListView1.Items.Add(item)
            Next
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.Columns.Add("ID","ID")
        Me.DataGridView1.Columns.Add("NAME","NAME")
        Me.DataGridView1.Tag = mcc.GetHashCode.ToString
    End Sub

    Private Sub BtnAddColl_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles BtnAddColl.Click
        mcc = New Collection
        Me.DataGridView1.Rows.Clear()
        Me.DataGridView1.Tag = mcc.GetHashCode.ToString
    End Sub

    Private Sub BtnSaveColl_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles BtnSaveColl.Click

        If Me.DataGridView1.Rows.Count > 0 Then
            mcc.Clear()
            For i As Integer = 0 To Me.DataGridView1.RowCount - 1
                If Me.DataGridView1.Rows(i).IsNewRow = False Then
                    mcc.Add(Me.DataGridView1.Rows(i),i.ToString)
                End If
            Next
            If mList.ContainsKey(Me.DataGridView1.Tag) Then
                mList.Item(Me.DataGridView1.Tag) = mcc
            Else
                mList.Add(mcc.GetHashCode.ToString,mcc)
            End If
            ShowList()
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If Me.ListView1.SelectedItems.Count > 0 Then
            Me.DataGridView1.Tag = Me.ListView1.SelectedItems.Item(0).Tag.ToString
            mcc = mList.Item(Me.DataGridView1.Tag)
            Me.DataGridView1.Rows.Clear()
            For Each mc As Object In mcc
                Me.DataGridView1.Rows.Add(CType(mc,DataGridViewRow))
            Next
        End If
    End Sub
End Class
原文链接:https://www.f2er.com/vb/259481.html

猜你在找的VB相关文章