2

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

    }

}
4

2 回答 2

1

在下面提到的代码的帮助下,我已经将 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();

    }

}

}

于 2013-06-13T09:18:38.923 回答
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();
于 2013-06-02T14:12:01.227 回答