2013-03-28 143 views
1

我有一个应用程序,我想从一个应用程序下载不同的用户的微博:合并结果的GridView

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. DataTable dtOld = GridView1.DataSource as DataTable; 2.解析args.Result到另一个DataTable对象。 3.合并上述两个? – gaurav

回答

1

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

0

我会从你的wblastID_DownloadStringCompleted方法中移除所有的gridview调用。相反,拥有一个可数据表的公共属性。由于每条语句都调用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 

如果您需要更具体的例子,让我知道