vb.netdatagridviewimagecolumn

Showing the same image repeatedly in a DataGridView without memory over-use


I think I'm doing something very stupid, but can't find an alternative!

I've got a list of files that my form is going to process, and am using a datagridview to show them all and my progress through the list. I've got a "waiting" "processing" and "done" image resource, and throw them all into a DataGridViewImageColumn.

For Each file As String In folderListing
     DataGridView1.Rows.Add(file, My.Resources.WaitingImg)

Next

Problem is, it seems to use a little tiny chunk of RAM for each of those images, and when I have 5,000 of them in a test run it used 4Gb...

How do I do this so it's only loading the resource once but displaying it in hundreds or thousands of places?


Solution

  • Don't use any property of My.Resources repeatedly. Every time you use such a property, it extracts the data from the resource and creates a new object. In any particular context, get the property once, assign to a variable and then use that variable multiple times, e.g.

    Dim waitingImg = My.Resources.WaitingImg
    
    For Each file As String In folderListing
         DataGridView1.Rows.Add(file, waitingImg)
    Next
    

    If you need to change the images, assign the resource properties to fields first and then use those fields to set the cell values as and when required.