简体   繁体   中英

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).

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;} }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM