且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#显示FQL结果DataGridView中(的WinForms)

更新时间:2023-12-06 17:19:28

C#的WinForms 使用 DataGridView的控制就像你在标题中指定的解决方案。






1)的修改你的 MyFriend 后,我改变了它一点点

 公共类MyFriends 
{
众长UID {搞定;组; }
公共字符串的用户名{获得;组; }
公共字符串FIRST_NAME {搞定;组; }
公共字符串姓氏{搞定;组; }
众长? FRIEND_COUNT {搞定;组; } //可空
公共字符串pic_big {搞定;组; }
}



2)DataGridView控件添加到您的窗体



3)反序列化 result.data ​​ code>使用 JsonConvert 到列表中。你也应该通过 result.data.ToString()或它会给一个例外。



4)绑定您的新名单 DataGridView.Datasource

 变种FB =新FacebookClient(的accessToken); 

VAR的查询=的String.Format(@SELECT UID,用户名,名字,姓氏,FRIEND_COUNT,pic_big
从用户其中uid IN(SELECT UID2从相知WHERE UID1 =我()) );

动态参数=新ExpandoObject();
parameters.q =查询;
动态结果= fb.Get(/ FQL,参数);

名单,LT; MyFriends> Q = JsonConvert.DeserializeObject<名单,LT; MyFriends>>(results.data.ToString());

dataGridView1.DataSource = Q;



结果:
使用C#显示FQL结果DataGridView中(的WinForms)


I have my FQL query and i have my access token. But how do i display FQL results in GRID VIEW using C# and ASP.NET?

FQL:

SELECT uid, username, first_name, last_name, friend_count, pic_big  
FROM user 
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) 

This is what i have tried:

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Facebook;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                var fb = new FacebookClient("{My Access Token}");

                dynamic result = fb.Get("/me");
                var name = result.name;

               MessageBox.Show("Hi " + name);
            }
        }
    }

I have realized that the data is returned from Facebook as JSON so i attempt to deserialize but still now luck.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Facebook;
using System.Dynamic;
using Newtonsoft.Json;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        public class MyFriends
        {
            public int uid {get; set;}
            public string username {get; set;}
            public string first_name {get; set;}
            public string last_name {get; set;}
            public int friend_count {get; set;}
            public string pic_big {get; set;}
        }


        private void button1_Click(object sender, EventArgs e)
        {
            var fb = new FacebookClient("2A64ZAIeJVIbdZAxXRZCwYf5Bg27OgZDZD");

            var query = string.Format("SELECT uid, username, first_name, last_name, friend_count, pic_big  FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");

            dynamic parameters = new ExpandoObject();
            parameters.q = query;
            dynamic results = fb.Get("/fql", parameters);

            results = JsonConvert.DeserializeObject<MyFriends>(results);

            //MessageBox.Show("Hi " + results);
        }
    }
}

C# Winforms solution using a DataGridView control like you specify in the title.


1) Modify your MyFriend class, I changed it a little bit

public class MyFriends
{
    public long uid { get; set; }            
    public string username { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public long? friend_count { get; set; }  //nullable
    public string pic_big { get; set; }
}

2) Add DataGridView control to your form

3) Deserialize your result.data using JsonConvert to a List. You should also pass result.data.ToString() or it will give an exception.

4) Bind your new list to DataGridView.Datasource

var fb = new FacebookClient("accessToken");

var query = string.Format(@"SELECT uid, username, first_name, last_name, friend_count, pic_big 
                            FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");

dynamic parameters = new ExpandoObject();
parameters.q = query;
dynamic results = fb.Get("/fql", parameters);

List<MyFriends> q = JsonConvert.DeserializeObject<List<MyFriends>>(results.data.ToString());

dataGridView1.DataSource = q;

Result: