如何在datagridview中显示所有行?

时间:2014-05-28 07:52:11

标签: c# wpf datagridview foreach datatable

 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

的功能

public DataTable 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中显示所有行而不是一行

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

你的代码总是会显示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;