프로그래밍언어/VB.NET

비베닷넷- 리스트뷰 가상모드

부산딸랑이 2013. 4. 30. 22:11




리스트뷰에 가상모드(VirtualMode) 란 속성이 있습니다.

대량의 데이터를 편집하기 유용합니다. 물론 제약이 따른긴 하지만 용도에따라 유용할것 같네요.

기존의 listview가 item을 하나씩 하나씩 추가하는데 가상모드는 listviewitem의 배열을 만들고 한번에 추가합니다.


간단하게 위소스처럼 virtual mode속성을 활성화시키고 사이즈를 정한뒤에 Listviewitem을 만들어 추가합니다.


순식간에 1000만개의 아이템이 들어있는 listview가 출력됩니다.

무척 빨라요!!


        For i = 0 To 100000

            ListView1.Items.Add(i)

        Next

이런식으로 추가한다면 10만개만 출력하려고해도 제 PC에서 6초가 걸립니다..

(1000만개 추가하려면 600초(10분)?? ㄷㄷㄷ 프로세스가 멈춰버리더군요..)

직접 한번 해보세요! 

이정도로 많은 데이터를 접할일이 없어서 모르고있었는데 무척 신기하네요.




아래 소스는 MSDN에서 제공하는 10000개의 숫자 제곱을 나타내는 소스입니다.

더보기



Public Class Form1

    Inherits Form

    Private myCache() As ListViewItem 'array to cache items for the virtual list

    Private firstItem As Integer 'stores the index of the first item in the cache

    Private WithEvents listView1 As ListView


    Public Shared Sub Main()

        Application.Run(New Form1)

    End Sub


    Public Sub New()

        'Create a simple ListView.

        listView1 = New ListView()

        listView1.View = View.Details

        listView1.Columns.Add("cnt")

        listView1.Columns.Item(0).Width = 200



        listView1.VirtualMode = True

        listView1.VirtualListSize = 10000

        listView1.Dock = DockStyle.Fill



        'Add ListView to the form.

        Me.Controls.Add(listView1)


        'Search for a particular virtual item.

        'Notice that we never manually populate the collection!

        'If you leave out the SearchForVirtualItem handler, this will return null.

        Dim lvi As ListViewItem = listView1.FindItemWithText("111111")


        'Select the item found and scroll it into view.

        If Not (lvi Is Nothing) Then

            listView1.SelectedIndices.Add(lvi.Index)

            listView1.EnsureVisible(lvi.Index)

           




        End If


    End Sub


    'The basic VirtualMode function.  Dynamically returns a ListViewItem

    'with the required properties; in this case, the square of the index.

    Private Sub listView1_RetrieveVirtualItem(ByVal sender As Object, ByVal e As RetrieveVirtualItemEventArgs) Handles listView1.RetrieveVirtualItem

        'Caching is not required but improves performance on large sets.

        'To leave out caching, don't connect the CacheVirtualItems event 

        'and make sure myCache is null.

        'check to see if the requested item is currently in the cache

        If Not (myCache Is Nothing) AndAlso e.ItemIndex >= firstItem AndAlso e.ItemIndex < firstItem + myCache.Length Then

            'A cache hit, so get the ListViewItem from the cache instead of making a new one.

            e.Item = myCache((e.ItemIndex - firstItem))

        Else

            'A cache miss, so create a new ListViewItem and pass it back.

            Dim x As Integer = e.ItemIndex * e.ItemIndex

            e.Item = New ListViewItem(x.ToString())

        End If



    End Sub


    'Manages the cache.  ListView calls this when it might need a 

    'cache refresh.

    Private Sub listView1_CacheVirtualItems(ByVal sender As Object, ByVal e As CacheVirtualItemsEventArgs) Handles listView1.CacheVirtualItems

        'We've gotten a request to refresh the cache.

        'First check if it's really neccesary.

        If Not (myCache Is Nothing) AndAlso e.StartIndex >= firstItem AndAlso e.EndIndex <= firstItem + myCache.Length Then

            'If the newly requested cache is a subset of the old cache, 

            'no need to rebuild everything, so do nothing.

            Return

        End If


        'Now we need to rebuild the cache.

        firstItem = e.StartIndex

        Dim length As Integer = e.EndIndex - e.StartIndex + 1 'indexes are inclusive

        myCache = New ListViewItem(length) {}


        'Fill the cache with the appropriate ListViewItems.

        Dim x As Integer = 0

        Dim i As Integer

        For i = 0 To length

            x = (i + firstItem) * (i + firstItem)

            myCache(i) = New ListViewItem(x.ToString())

        Next i


    End Sub


    'This event handler enables search functionality, and is called

    'for every search request when in Virtual mode.

    Private Sub listView1_SearchForVirtualItem(ByVal sender As Object, ByVal e As SearchForVirtualItemEventArgs) Handles listView1.SearchForVirtualItem

        'We've gotten a search request.



        'In this example, finding the item is easy since it's

        'just the square of its index.  We'll take the square root

        'and round.

        Dim x As Double = 0

        If [Double].TryParse(e.Text, x) Then 'check if this is a valid search

            x = Math.Sqrt(x)

            x = Math.Round(x)

            e.Index = Fix(x)

        End If

        'Note that this only handles simple searches over the entire

        'list, ignoring any other settings.  Handling Direction, StartIndex,

        'and the other properties of SearchForVirtualItemEventArgs is up

        'to this handler.

    End Sub


End Class


역시 순식간에 띄웁니다.


자세한내용은 

http://msdn.microsoft.com/ko-kr/library/system.windows.forms.listview.retrievevirtualitem.aspx 

http://www.experts-exchange.com/Programming/Languages/.NET/Q_22518005.html

에서 확인!