2013-10-28 32 views
0

我試圖設置圖標,按我的分貝vaules是1,0 我是用這個代碼的datagridview細胞(從該網站複製!)設置圖標,每分貝datagridview的單元格的值

foreach (DataGridViewRow row in DataGridView1.Rows) 

{ DataGridViewImageCell cell = row.Cells[8] as DataGridViewImageCell; 

if row.Cells[8]==1) 
       cell.Value = (System.Drawing.Image)Properties.Resources.ok; 

if row.Cells[8]==0) 
       cell.Value = (System.Drawing.Image)Properties.Resources.notOK; 

} 

但是編譯器顯示此錯誤:

System.NullReferenceException was unhandled by user code 
Message="Object reference not set to an instance of an object. 

該代碼有什麼問題?

+0

指定圖像和檢查單元格的值相同? – Sathish

+0

4例如,我想爲1值設置綠色刻度圖標和0值的紅色停止圖標,如何在datagridview中綁定數據後或從dataTable中的數據庫中獲取數據後,如何通過代碼執行此操作? tanx –

+0

你試過我的回答 – Sathish

回答

0

DT.Columns.Add("c",System.Windows.Forms.DataGridViewImageCell);

for (int i = 0; i < DT.Rows.Count; i++) { if (DT.Rows[i][8].ToString() == "1")

DT.Rows[i]["a"] = (System.Drawing.Image)Properties.Resources.ok;

else

row.Cells[8].Value = (System.Drawing.Image)Properties.Resources.notOK;

0

試試這個。我想你想這

foreach (DataGridViewRow row in DataGridView1.Rows) 

{ 

if (row.Cells[8]==1) 
    { 
     row.Cells[8]=new DataGridViewImageCell(); 
     row.Cells[8].Value = (System.Drawing.Image)Properties.Resources.ok; 
    } 
if (row.Cells[8]==0) 
    { 
     row.Cells[8]=new DataGridViewImageCell(); 
     row.Cells[8].Value = (System.Drawing.Image)Properties.Resources.notOK; 
    } 
} 

編輯

dataGridView1.Columns.Add("a", "Image"); 
      for (int i = 0; i < dataGridView1.Rows.Count; i++) 
      { 
       if (dataGridView1.Rows[i].Cells[8].ToString() == "1") 
       { 
        dataGridView1.Rows[i].Cells["a"] = new DataGridViewImageCell(); 
        dataGridView1.Rows[i].Cells["a"].Value = (System.Drawing.Image)Properties.Resources.Config; 
       } 
       else 
       { 
        dataGridView1.Rows[i].Cells["a"].Value = (System.Drawing.Image)Properties.Resources.Config; 

       } 
      } 
+0

Tanx爲你答覆親愛的SATSON,我發現這個問題的解決方案:當datagridview的數據綁定完成時,必須創建一個新的DataGridViewImageCell類型列並填充它每個數字列,然後隱藏數列,但我不知道翻譯它爲C#代碼,我試試此代碼: –

+0

@farid哪個代碼 – Sathish

相關問題