1

我有一个应用程序,我想从应用程序下载不同用户的推文:

DataTable dt = obj.GetTableSP("GetAllBioOrderByNewest");

foreach (DataRow dr in dt.Rows)
{
    WebClient wb = new WebClient();
    Uri myUri = new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&count=10&screen_name=" + dr["TwitterHandle"].ToString());
    wb.DownloadStringAsync(myUri);
    wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wblastID_DownloadStringCompleted);
}

在这里我如何在gridview中绑定结果:

public void wblastID_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
{
    try
    {
        XElement xmlTweets = XElement.Parse(args.Result);
        GridView1.DataSource = (from tweet in xmlTweets.Descendants("status")
                                select new Tweet
                                {
                                    ID = tweet.Element("id").Value,
                                    Message = tweet.Element("text").Value,
                                    UserName = tweet.Element("user").Element("screen_name").Value,
                                    TwitTime = tweet.Element("created_at").Value
                                }).ToList();


        GridView1.DataBind();
        if (GridView1.Rows.Count < 10)
        {
            viewmore.Visible = false;
        }
    }
    catch
    {
        MessageBox.Show("Error downloading tweets");
    }
}

但这里的问题是,我只从数据表中获取最后一个用户的推文。

我想要的是,所有用户的组合结果dt并将其显示在 gridview 中。

4

2 回答 2

1

不要在wblastID_DownloadStringCompleted事件中绑定你的网格,因为你在循环中调用它,foreach 所以在每次迭代中它都绑定了新的细节。您必须创建一个新集合(listdatatable),然后在foreach 循环之后将您的 gridview 与新集合绑定。

于 2013-03-28T11:51:39.680 回答
0

我将从您的 wblastID_DownloadStringCompleted 方法中删除所有 gridview 调用。相反,拥有一个作为数据表的公共属性。由于 for each 语句调用 wblastID_DownloadStringCompleted 方法,只需附加数据表即可。for 语句完成后,即绑定数据源。一些伪代码:

someMethod
 for each twitterName as strin in myTable
     dim myFoundTweets as datatable
     dim myTweetName as string = "Your API Call To Get The name"

     myFoundTweets = addMe(myFoundTweets , myTweetName )

 next

  'now bind the source
end Metod


private sub addMe(byval myTweetName as string, byVal myTable as table)
   'parse the string here
   'add the values to a table or list of object
   'return it
end sub

如果您需要更具体的示例,请告诉我

于 2013-03-28T11:57:34.297 回答