我正在尝试以编程方式从2个不同的数据源构建DataGrid。我有一个List和一个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并且它们正在被适当地着色,但行中的每个单元格都不包含数据?我最终得到的是几个空白行,它们的背景属性已设置。
有人有任何想法吗?
提前致谢。
答案 0 :(得分:0)
尝试在DataGridViewRow上使用BeginEdit和EndEdit
答案 1 :(得分:0)
这是我采取这样做的解决方案。
为了演示目的我构建了一个EbayItem类: -
public class EbayItem
{
public EbayItem()
{
}
public EbayItem(string sku, int id, string product, int ebayqty, int stockqty)
{
SKU = sku;
ID = id;
ProductName = product;
ebayQty = ebayqty;
stockQty = stockqty;
}
public string SKU { get; set; }
public int ID { get; set; }
public string ProductName { get; set; }
public int ebayQty { get; set; }
public int stockQty { get; set; }
public int difference
{
get { return stockQty - ebayQty; }
set { difference = value; }
}
}
在Windows窗体(Form1_Load)中,我创建了一些测试条目并将它们添加到List对象中。最后一个条目看起来买2枚手榴弹,但库存为零: -
List<EbayItem> Inventory = new List<EbayItem>();
Inventory.Add(new EbayItem("SKU1", 1, "Ski-Mask", 1, 10));
Inventory.Add(new EbayItem("SKU2", 2, "Shotgun", 1, 10));
Inventory.Add(new EbayItem("SKU3", 3, "Rounds", 5, 10));
Inventory.Add(new EbayItem("SKU4", 4, "Grenade", 2, 0));
我没有手动向我的DataGridView添加行,而是创建一个BindingSource并用我的库存填充它: -
BindingSource bs = new BindingSource(Inventory, null);
接下来,为我的DGV分配数据源,将其设置为Binding对象。这意味着对列表对象所做的任何更改都会自动波及DGV: -
dataGridView1.DataSource = bs;
现在剩下的就是利用DGV的CellFormatting事件句柄。我用它来突出显示股票差异为零或更小的任何行: -
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
if (rowIndex >= 0 && colIndex >= 0)
{
DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
int difference = (int)theRow.Cells[5].Value;
if (difference <= 0)
{
theRow.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
theRow.DefaultCellStyle.ForeColor = System.Drawing.Color.White;
}
}
}
希望这有帮助。