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中具有category_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)
{

字符串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中显示所有行而不是一行

最佳答案

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

10-08 09:46