2013-02-22 39 views
0

我试图以编程方式从2个不同的数据源构建DataGrid。我有一个List和一个DataGrid。问题不在于我的数据处理,而在于DataGridViewRow对象的值。这里是我的代码:构建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

可能的重复[为什么我看不到DataGridViewRow添加到DataGridView?](http://stackoverflow.com/questions/5698356/why-cant-i-see-the-datagridviewrow-added-to- a-datagridview) – 2013-02-22 10:51:15

+0

是的,在那个问题中标记为解决方案的答案就是我所期待的。我会尽快专门回答这个问题。 – Dan 2013-02-22 11:21:33

+0

当我有机会时,我会添加一种更清洁的方法。 – Derek 2013-02-22 12:05:26

回答

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的),我在里面创建了几个测试项目,并将它们添加到列表对象。最后一项看起来买了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; 
       } 
      } 

     } 

希望这会有所帮助。

+0

伟大的解决方案。我非常喜欢数据绑定,而不是手动尝试构建表格。 – Dan 2013-02-26 20:03:34

0

尝试使用BeginEdit和EndEdit中的一个DataGridViewRow

+0

我在DataGridViewRow对象上没有可用的BeginEdit和EndEdit方法。然而,我在DataGridView对象上做了一些尝试,但没有成功。 – Dan 2013-02-22 11:07:19

+0

Nvm,我找到了解决方案。我会尽快发布。 – Dan 2013-02-22 11:20:44