Search code examples
c#winformsdatagridviewvstooutlook-addin

Winforms list to datagridview but no content


I need a little help using winforms.

I have a datagridview and in code behind I set its datasource:

public ContactExporter(IEnumerable<ContactItem> contacts)
{
    InitializeComponent();
    BindingList<ContactItem> contactItems = new BindingList<ContactItem>(contacts.ToList());
    contactsGrid.DataSource = contactItems;
}

ContactItem documentation: https://msdn.microsoft.com/en-us/library/office/ff867603.aspx

I have three columns with the DataPropertyNames LastName, FirstName and CompanyName, but every single cell is empty (rows are existing).


Solution

  • ContactItem and _ContactItem are interfaces belong to Microsoft.Office.Interop.Outlook. You cannot use them to binding data on DataGridView.

    Solution:

    Create a Class with such properties that you need to show on DataGridView. Then, you wrap ContactItem by that Class.

    public class MyContactItem : ContactItem
    {
        public string MyFirstName { get{ return FirstName;} }
        ...
    }