public ProductsList()
{
InitializeComponent();
CreateProductList();
DataTable dt = DataTableProductList(productList);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
DataGridProducts.DataSource = bs;
this.DataGridProducts.Columns[0].Width = 50;
this.DataGridProducts.Columns[1].Width = 150;
this.DataGridProducts.Columns[2].Width = 100;
this.DataGridProducts.Columns[3].Width = 50;
this.DataGridProducts.Columns[4].Width = 200;
this.DataGridProducts.Columns["ID"].Width = 50;
}
private void CreateProductList()
{
try { productList = ProductDB.GetProductList(); }
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}
public static DataTable DataTableProductList(List<Product> prodList)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Product Name");
dataTable.Columns.Add("Product Price");
dataTable.Columns.Add("Quantity");
dataTable.Columns.Add("Product Listed Date");
foreach(var p in prodList)
{
var row = dataTable.NewRow();
row["ID"] = p.ProductId;
row["Product Name"] = p.ProductName;
row["Product Price"] = p.ProductPrice;
row["Quantity"] = p.ProductQuantity;
row["Product Listed Date"] = p.ProductListedDate;
dataTable.Rows.Add(row);
}
return dataTable;
}
The truth is I was looking for this solution and your question pointed me the right direction to a solution that worked for me so I decided to share.