2013-03-28 141 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 

如果您需要更具體的例子,讓我知道