我正在尝试从 2 个不同的数据源以编程方式构建 DataGrid。我有一个列表和一个数据网格。问题不在于我的数据处理,而在于 DataGridViewRow 对象的值。这是我的代码:
protected void buildGrid()
{
dgResults.Columns.Add( "sku", "SKU" );
dgResults.Columns.Add( "itemID", "Item ID" );
dgResults.Columns.Add( "productName", "Product Name" );
dgResults.Columns.Add( "eBayQty", "eBay Qty" );
dgResults.Columns.Add( "stockQty", "Stock Qty" );
dgResults.Columns.Add( "difference", "Difference" );
//Add the eBayItem data to the table
foreach ( string[] eBayItem in ebayItems )
{
string SKU = eBayItem[1].ToString();
int eBayQty = Convert.ToInt32(eBayItem[2]);
string ProductName = "";
int stockQty = 0;
int qtyDifference = 0;
DataRow[] rows = dbData.Select( "sku ='" + SKU + "'" );
if (rows.Length == 1) {
stockQty = Convert.ToInt32( rows[0]["quantity"] );
ProductName = rows[0]["ProductName"].ToString();
}
qtyDifference = stockQty - eBayQty;
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.SetValues( SKU, eBayItem[0].ToString(), ProductName, eBayQty, stockQty, qtyDifference );
if ( qtyDifference != 0 || eBayQty > stockQty )
{
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
dgvr.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
}
else if ( stockQty > eBayQty )
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
else
dgvr.DefaultCellStyle.BackColor = System.Drawing.Color.GreenYellow;
dgResults.Rows.Add(dgvr);
}
}
这些行正在添加到 DataGrid 并且它们被适当地着色,但是行中的每个单元格都不包含数据?我最终得到的只是几个设置了背景属性的空白行。
有人有任何想法吗?
提前致谢。