Display FQL results in DataGridView using C# (WinForms)

随声附和 提交于 2019-12-20 03:19:22

问题


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:


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:



来源:https://stackoverflow.com/questions/20825015/display-fql-results-in-datagridview-using-c-sharp-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!