Vishal Suthar
Vishal Suthar

Reputation: 17194

Combine result in gridview

I have an application where I want to download tweets of different user from an application:

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);
}

And Here how I bind the result in 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");
    }
}

But here the problem is, I only get the tweet of the last user from the datatable.

What I want is, combined result of all the users from dt and display it in gridview.

Upvotes: 1

Views: 140

Answers (2)

jason
jason

Reputation: 3615

I would remove all gridview calls from your wblastID_DownloadStringCompleted method. Instead, have a public property that is a datatable. As the for each statement calls the wblastID_DownloadStringCompleted method, just append the datatable. After the for statements finishes, thats when you bind the data source. Some psuedo code:

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

If you need a more specific example, let me know

Upvotes: 0

Rohit Vyas
Rohit Vyas

Reputation: 1969

Don't Bind your grid in wblastID_DownloadStringCompleted event as you are calling it in a foreach loop so in each iteration it is binded with new detail. You have to create a new collection (list or datatable) and then after the foreach loop bind you gridview with new collection.

Upvotes: 1

Related Questions