我正在尝试将 Gmail 的联系人添加到 C# 桌面表单应用程序中,为此我使用了 google APi,我想在按下按钮时在 gridview 中显示 gmail 的联系人。但是当按下按钮时,网格视图中不会显示任何内容。
代码粘贴在下面。
请让我知道并帮助我解决此问题。
public class MyClass
{
public int Id { get; set; }
public string Email { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void FetchContactList()
{
// Define string of list
List<string> lstContacts = new List<string>();
// Below requestsetting class take 3 parameters applicationname, gmail username, gmail password. Provide appropriate Gmail account details
RequestSettings rsLoginInfo = new RequestSettings("", "suryabg2000@gmail.com", "XXXXXX");
rsLoginInfo.AutoPaging = true;
ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
// fetch contacts list
Feed<Contact> feedContacts = cRequest.GetContacts();
// looping the feedcontact entries
try
{
foreach (Contact gmailAddresses in feedContacts.Entries)
{
// Looping to read email addresses
foreach (EMail emailId in gmailAddresses.Emails)
{
lstContacts.Add(emailId.Address);
}
}
// finally binding the list to gridview defined in above step
// dataGridView1.DataSource = lstContacts;
////dataGridView1.DataBind();
////dataGridView1.DataSource = dataGridView1;
// dataGridView1.Show();
}
catch (Exception)
{
MessageBox.Show("Error Please enter the correct credentials","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
//throw;
}
}
private void button1_Click(object sender, EventArgs e)
{
List<MyClass> lstContacts = new List<MyClass>();
//lstContacts.Add(new MyClass() { Id = 1, Email = "def@gmail.com" });
//lstContacts.Add(new MyClass() { Id = 2, Email = "def@gmail.com" });
//lstContacts.Add(new MyClass() { Id = 3, Email = "ghi@gmail.com" });
dataGridView1.DataSource = new BindingSource(lstContacts, null);
dataGridView1.Show();
}
}