2013-02-22 94 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

相關問題