我有一个应用程序,我想从应用程序下载不同用户的推文:
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中。
答案 0 :(得分:1)
不要在wblastID_DownloadStringCompleted
事件中绑定您的网格,因为您在foreach
循环中调用它,因此在每次迭代中都会绑定新的详细信息。您必须创建一个新集合(list
或datatable
),然后在foreach
循环后将gridview绑定到新集合。
答案 1 :(得分: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
如果您需要更具体的示例,请与我们联系