【问题标题】:Get Gmail Contacts from into C# desktop form将 Gmail 联系人从 C# 桌面表单中获取
【发布时间】:2013-06-02 13:43:10
【问题描述】:

我正在尝试将 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();

    }

}

【问题讨论】:

    标签: c# gmail


    【解决方案1】:
    public class MyClass
    {
        public int Id { get; set; }
        public string Email { get; set; }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        List<MyClass> lstContacts = new List<MyClass>();
        //need to add items to list
        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 = lstContacts;
        dataGridView1.Show();
    
     }
    

    我建议您先单步执行代码,这应该可以让您更好地了解导致网格视图空结果的确切原因。调用 Google Contacts API 或将数据绑定到您的 gridview 控件时可能会出现问题。

    但是仅仅通过查看您的点击事件处理程序,我不禁要问您为什么注释掉 lstContacts.Add 行。列表lstContacts 仍然是空的,不是吗?

    另外,我会改用dataGridView1.DataSource = lstContacts 来进行数据绑定。

    编辑: 该代码当然对我有用。这里显示了 3 行。

    例子:

    使用包装类

    public class StringValue
    {
        public StringValue(string s)
        {
            _value = s;
        }
        public string Value { get { return _value; } set { _value = value; } }
        string _value;
    }
    List<StringValue> lstContacts = new List<StringValue>();
    lstContacts.Add("your email address");
    dataGridView1.DataSource = lstContacts;
    dataGridView1.Show();
    

    使用数据表

    DataTable dt = new DataTable();
    dt.Columns.Add("Email Address");
    
    dt.Rows.Add(new object[] { "def@gmail.com" });
    dt.Rows.Add(new object[] { "def@gmail.com" });
    dt.Rows.Add(new object[] { "def@gmail.com" });
    dataGridView1.DataSource = dt;
    dataGridView1.Show();
    

    【讨论】:

    • 我是 C# 编程新手!!!我已经使用了您对 dataGridView!.DataSource = lstContacts 的建议...但 lstContacts 仍然是空的。它没有在网格视图中向我显示任何内容!!!请帮帮我!
    • 如果取消注释这些行会发生什么?或者你可以尝试使用不同类型的新列表,比如字符串类型。列表 lstTest = 新列表(); lstTest.Add("abc"); dataGridView1.DataSource = lstTest;
    • 试一试,看看是否能给你带来任何结果。
    • 我已尝试传递 RequestSettings rsLoginInfo = new RequestSettings("",emailtb.text,tbpassword.text); 的参数现在它给了我联系人的长度......而网格视图只给了我一列,那就是长度!!!!
    • 是的,这就是预期的!因为您正在使用 List.
    【解决方案2】:

    在下面提到的代码的帮助下,我已将 gmail 的联系人获取到 C# 桌面应用程序。为此目的使用了 GOOGLE API!!! 下面粘贴了正确且有效的数据集代码。

    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("", textBox1.Text, textBox2.Text);
            rsLoginInfo.AutoPaging = true;
            ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
    
            // fetch contacts list
            Feed<Contact> feedContacts = cRequest.GetContacts();
    
    
            //dataGridView1.ColumnCount = 1;
            //dataGridView1.Columns[0].Name = "Product ID";
    
    
    
            // looping the feedcontact entries
            try
            {
               // dataGridView1.Columns.Add("Name", "Name");
                RichTextBox rtb = new RichTextBox();
                string email = "";
                DataTable dt = new DataTable();
                dt.Columns.Add("Email Address");
                foreach (Contact gmailAddresses in feedContacts.Entries)
                {
                    // Looping to read email addresses
                    foreach (EMail emailId in gmailAddresses.Emails)
                    {
                       dt.Rows.Add(new object[] {email=emailId.Address});
                       dataGridView1.DataSource = dt;  
                    }
    
                    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)
        {
    
            fetchContactList();
    
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2018-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      相关资源
      最近更新 更多