Faster Way to Loop Contacts to Display them in a DataGridView?

Answered Faster Way to Loop Contacts to Display them in a DataGridView?

  • Wednesday, August 15, 2012 8:37 PM
     
      Has Code

    I'm using the code inserted below to grab what is currently 6000+ contacts from a public contacts folder & then display them in a DataGridView on a form.  

    The program just hangs while it tries to run through these thousands of contacts & display them.  

    Is there a faster, better way to loop & get all the data from these contacts?     

    I am working in Visual Studio 2010 developing an add-in for Outlook 2010.

    Outlook.Folder fldFullPathContacts = common.Folder.GetByPath(Properties.Settings.Default.PFPathContacts, Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
    
                    Outlook.Items ContactItems = fldFullPathContacts.Items;            
                    dgvUnassociatedContacts.Rows.Clear();
    
                    int iGarbageCount = 0;
    
                    foreach (Object obj in ContactItems)
                    {
                        if (obj is Outlook.ContactItem)
                        {
                            Outlook.ContactItem oc = ((Outlook.ContactItem)obj);
                            
                            dgvUnassociatedContacts.Rows.Add(oc.EntryID, oc.FullName, oc.Email1Address, oc.CompanyName);
                            oc = null;
                        }
    
                        iGarbageCount++;
    
                        if (iGarbageCount > 100)
                        {
                            iGarbageCount = 0;
    
                            GC.Collect();
    
                            GC.WaitForPendingFinalizers();
    
                            GC.Collect();
    
                            GC.WaitForPendingFinalizers();  //tried a second 
                        }
                    }

All Replies

  • Wednesday, August 15, 2012 8:56 PM
    Moderator
     
     Answered
    Using a Table of the folder contents would be far quicker.
     
    Those were added to the Outlook object model in Outlook 2003. You'd use Folder.GetTable() to get the Table object, which can be filtered. You can also set up the table colums to only load properties in which you're interested, which also speeds things up.
     
    You should see an order of magnitude improvement or more over iterating an Items collection.
     
    If you're using Redemption you can query a MAPITable for an RDOFolder's RDOItems collection and use the ExecSQL() method to return an ADO recordset, which some grids can directly accept. That's lightning fast.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Nerds" <=?utf-8?B?TmVyZHM=?=> wrote in message news:9f2fb3c9-6980-4048-9aab-972fbb0704b0...

    I'm using the code inserted below to grab what is currently 6000+ contacts from a public contacts folder & then display them in a DataGridView on a form.  

    The program just hangs while it tries to run through these thousands of contacts & display them.  

    Is there a faster, better way to loop & get all the data from these contacts?     

    I am working in Visual Studio 2010 developing an add-in for Outlook 2010.

    Outlook.Folder fldFullPathContacts = common.Folder.GetByPath(Properties.Settings.Default.PFPathContacts, Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
    
                    Outlook.Items ContactItems = fldFullPathContacts.Items;            
                    dgvUnassociatedContacts.Rows.Clear();
    
                    int iGarbageCount = 0;
    
                    foreach (Object obj in ContactItems)
                    {
                        if (obj is Outlook.ContactItem)
                        {
                            Outlook.ContactItem oc = ((Outlook.ContactItem)obj);
                            
                            dgvUnassociatedContacts.Rows.Add(oc.EntryID, oc.FullName, oc.Email1Address, oc.CompanyName);
                            oc = null;
                        }
    
                        iGarbageCount++;
    
                        if (iGarbageCount > 100)
                        {
                            iGarbageCount = 0;
    
                            GC.Collect();
    
                            GC.WaitForPendingFinalizers();
    
                            GC.Collect();
    
                            GC.WaitForPendingFinalizers();  //tried a second 
                        }
                    }


    Ken Slovak MVP - Outlook
    • Marked As Answer by Nerds Wednesday, August 15, 2012 8:59 PM
    •  
  • Wednesday, August 15, 2012 9:00 PM
     
     
    Thanks for the help & the quick response
  • Thursday, August 16, 2012 12:22 AM
     
     
    Also keep in mind that there is no point loading the comntents of the whole folder (Outlook never does that). Only load enough rows to display to the user.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!