【问题标题】:How to show all the rows in datagridview?如何显示datagridview中的所有行?
【发布时间】:2014-07-17 07:53:56
【问题描述】:
 DataTable dt = db.getProductIdFromCategoriesId(categories_id);

 foreach (DataRow row in dt.Rows)
 {
     string products_id = row["products_id"].ToString();
     DataTable dt5 = db.FillDataGridfromTree(int.Parse(products_id));
     show_products.ItemsSource = dt5.DefaultView;
 }

此代码在 datagridview 中一一显示
但我想一次性在datagridview中显示所有具有categories_id的产品行 这是databasecore类中的函数FillDataGridfromTree,它的对象是db

public DataTable FillDataGridfromTree(int product_Id)       
{            
        string CmdString = string.Empty;

        using (SqlCeConnection con = new SqlCeConnection(ConString))
        {
            CmdString = "SELECT products.product_id as ID, products.remote_products_id as Remote_ID, products_description.products_name as name,products.products_model as model,products.manufacturers_id as manufacturersId,products.products_image as Image,products.products_price as Price,products.products_weight as Weight,products.products_date_added as dateAdded,products.products_last_modified as lastModified,products.products_date_available as dateAvailable,products.products_status as status,products.products_tax_class_id as taxClass FROM products INNER JOIN products_description ON products.product_id=products_description.products_id where products_description.language_id=1 and products_description.products_id=" + product_Id;
            SqlCeCommand cmd = new SqlCeCommand(CmdString, con);
            SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmd);
            DataTable dt = new DataTable("products");
            adapter.Fill(dt);
            //show_products.ItemsSource = dt.DefaultView;
            return dt;    
        }
}

这是我获取product_id的函数

公共数据表 getProductIdFromCategoriesId(int categories_id) {

string CmdString = string.Empty;

        using (SqlCeConnection con = new SqlCeConnection(ConString))
        {
            CmdString = "SELECT products_id FROM products_to_categories where categories_id=" + categories_id;
            SqlCeCommand cmd = new SqlCeCommand(CmdString, con);
            DataTable dt = new DataTable();
            SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmd);
            adapter.Fill(dt);
            return dt;
        }
    }

如何在datagridview中显示所有行而不是一行

【问题讨论】:

  • 这是我做一件事的代码。给我做其他事情的代码。真的吗?请阅读 Stack Overflow 帮助中心的 How do I ask a good question 页面,以查看有关本网站提问的一些指南。

标签: c# wpf datagridview foreach datatable


【解决方案1】:

您的代码总是会显示 category_id 的最后一行,这是因为您在循环内分配了 ItemsSource。我已更改顶部以执行您想要的操作:

DataTable dt = db.getProductIdFromCategoriesId(categories_id);
List<DataRow> ProductList = new List<DataRow>();

foreach (DataRow row in dt.Rows)
{
   string products_id = row["products_id"].ToString();
   DataTable dt5 = db.FillDataGridfromTree(int.Parse(products_id));

  if(dt5.Rows.Count > 0)
  {
      ProductList.AddRange(dt5.Select().ToList());
  }
}

show_products.ItemsSource = ProductList.CopyToDataTable().DefaultView;

【讨论】:

  • 它在运行时出错 {"The source contains no DataRows."}
  • 在将行添加到集合之前,我已经编辑了代码以检查行。再次复制并尝试
  • thanx 但我的代码可以工作,先生。 GIVE-ME-CHICKEN 帮了我
【解决方案2】:

已更改尝试将您的 foreach 循环更改为:

DataTable dt = db.getProductIdFromCategoriesId(categories_id);

DataTable dt5 = new Datatable();

 foreach (DataRow row in dt.Rows)
 {
     string products_id = row["products_id"].ToString();
     dt5.Merge(db.FillDataGridfromTree(int.Parse(products_id)));
 }

show_products.ItemsSource = dt5.DefaultView;

【讨论】:

    猜你喜欢
    • 2012-12-03
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2014-10-04
    • 2010-11-17
    相关资源
    最近更新 更多