I want to get the contacts of outlook to C# form in grid view. I have used the Microsoft outlook 12.0 Object Library 9.3 version. I am trying to get the contacts displayed in gridview as the getcontact button is clicked.
Code is pasted below; kindly check it and help me out to solve the issue.
private void button1_Click(object sender, EventArgs e)
{
GetContacts();
}
public DataSet GetContacts()
{
DataSet ds = new DataSet();
ds.Tables.Add("Contacts");
ds.Tables[0].Columns.Add("Email");
ds.Tables[0].Columns.Add("FirstName");
ds.Tables[0].Columns.Add("LastName");
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj;
MAPIFolder Folder_Contacts;
outlookObj = new Microsoft.Office.Interop.Outlook.Application();
Folder_Contacts =
(MAPIFolder)outlookObj.Session.GetDefaultFolder(
OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;
for (int i = 0; i < OutlookItems.Count; i++)
{
Microsoft.Office.Interop.Outlook.ContactItem contact =
(Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i + 1];
DataRow dr = ds.Tables[0].NewRow();
dr[0] = contact.Email1Address;
dr[1] = contact.FirstName;
dr[2] = contact.LastName;
ds.Tables[0].Rows.Add(dr);
dataGridView1.DataSource = dr;
richTextBox1.Text = dr.ToString();
}
dataGridView1.Show();
return ds;
}