Display FQL results in DataGridView using C# (WinF

2019-02-25 15:27发布

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);
        }
    }
}

1条回答
beautiful°
2楼-- · 2019-02-25 15:44

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: enter image description here

查看更多
登录 后发表回答