如何在DataGridView中显示所有行?

3
 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 中的数据,但我希望一次性将 categories_id 相同的所有产品行在 datagridview 中显示出来。这是数据库核心类中 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;    
        }
}

这是我获取产品ID的函数。
``` public DataTable getProductIdFromCategoriesId(int categories_id) { string CmdString = string.Empty; ```
该函数用于根据分类ID获取产品ID。
        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 帮助中心的 如何提出好问题 页面,了解一些在该网站上提问的指南。 - Sheridan
2个回答

3

更改 尝试将您的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;

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;

运行时出现错误 {"源中不包含任何数据行。"} - Striker
我已经编辑了代码,以在将它们添加到集合之前检查行。请再次复制并尝试。 - Bayeni
谢谢,但我的代码已经可以运行了,GIVE-ME-CHICKEN先生帮了我。 - Striker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接