【问题标题】:Combine result in gridview在gridview中合并结果
【发布时间】:2013-03-28 11:46:27
【问题描述】:

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

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 中。

【问题讨论】:

  • 这样的东西能行吗?不知道你的问题对我来说有点不清楚。 1. DataTable dtOld = GridView1.DataSource as DataTable; 2. 将 args.Result 解析为另一个 DataTable 对象。 3.合并以上两个?

标签: c# asp.net linq list datatable


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我会从您的 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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2019-08-06
      相关资源
      最近更新 更多