【问题标题】:How can I add multiple statements to DataGridView DataSource如何将多个语句添加到 DataGridView 数据源
【发布时间】:2012-05-03 22:57:13
【问题描述】:

在系统中,dt 获取 url,然后将它们放入 getco.... 函数中,然后调用 sql 语句,将其添加到 gridview 数据源。但它会获取最后一个 url 的项目,所以它会覆盖。我该如何解决?

     for (int i = 0; i < dt.Rows.Count; i++)
     {
         row = dt.Rows[i];
         GridView1.DataSource = db.getComboxedCombinedRSS( row[0].ToString());
     }

     GridView1.DataBind();




public DataSet getComboxedCombinedRSS(string url)
{
    //SQL string to count the amount of rows within the OSDE_Users table
    //string sql = "SELECT [RSS_Title], [RSS_ID], R.Syndication, R.Category FROM RSS AS R INNER JOIN CombinedFeeds AS C ON  C.URL = R.URL  WHERE C.Name='" +name+" ' ORDER BY RSS_Date desc";
    string sql="SELECT top 30 [RSS_Title], [RSS_ID], Syndication, Category FROM RSS where URL= '"+url+"' order by RSS_DATE";
    SqlDataAdapter adapt = new SqlDataAdapter(sql, Connect());
    DataSet ds = new DataSet();
    adapt.Fill(ds);

    // result of query filled into datasource
    adapt.Dispose();

    closeConnection();
    return ds;
}

【问题讨论】:

  • 你的循环正在覆盖它。处理它
  • 只有 leventkalay92 可以做到这一点:) 机制应该是什么?将每个结果集添加到 dgv?只保留第一个?保留一些而忽略其余的?你只告诉了你面临的麻烦,从来没有告诉过要求..

标签: c# data-binding datagridview datasource


【解决方案1】:
 DataSet ds = new DataSet();
 for (int i = 0; i < dt.Rows.Count; i++)
     {
         row = dt.Rows[i];
        db.getComboxedCombinedRSS( row[0].ToString(), ds);
     }
     GridView1.DataSource = ds;
     GridView1.DataBind();




public void getComboxedCombinedRSS(string url, DataSet ds)
{
    //SQL string to count the amount of rows within the OSDE_Users table
    //string sql = "SELECT [RSS_Title], [RSS_ID], R.Syndication, R.Category FROM RSS AS R INNER JOIN CombinedFeeds AS C ON  C.URL = R.URL  WHERE C.Name='" +name+" ' ORDER BY RSS_Date desc";
    string sql="SELECT top 30 [RSS_Title], [RSS_ID], Syndication, Category FROM RSS where URL= '"+url+"' order by RSS_DATE";
    SqlDataAdapter adapt = new SqlDataAdapter(sql, Connect());

    adapt.Fill(ds);

    // result of query filled into datasource
    adapt.Dispose();

    closeConnection();

}

【讨论】:

  • 谢谢,但它没有添加第二个,所以 else 语句不起作用?
  • RSS_ID 是 RSS_DATE 的主键吗?
  • getcomboxed 函数中存在错误,它返回 ds 但 void 函数我很困惑
猜你喜欢
  • 2017-02-07
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多