2012-11-02 120 views
0

我試圖根據單元格中的值更改顏色,完成或不完整,但由於某種原因,它說「顏色」在當前上下文中不存在。Datagridview顏色變化

是否有我應該使用的系統項目或什麼?

如果任何人有任何替代我想要做的,也將不勝感激。

foreach (DataGridViewRow row in dtaFinished.Rows) 
     { 
      string RowType = row.Cells[4].Value.ToString(); 

      if (RowType == "Completed") 
      { 
       row.DefaultCellStyle.BackColor = Color.Green; //Error on these lines 
       row.DefaultCellStyle.ForeColor = Color.White; //Error on these lines 
      } 
      else if (RowType == "Incomplete") 
      { 
       row.DefaultCellStyle.BackColor = Color.Yellow; 
       row.DefaultCellStyle.ForeColor = Color.Black; 
      } 
     } 

回答

1

使用下面的命名空間:

using System.Drawing;

希望這會有所幫助。

0

你好你可以發現你的answere Here

0

我在一個項目中使用這個前陣子: -

private void dgvOutstandingReports_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
      { 
       int colIndex = e.ColumnIndex; 
       int rowIndex = e.RowIndex; 


       if (rowIndex >= 0 && colIndex >= 0) 
       { 
        DataGridViewRow theRow = dgvOutstandingReports.Rows[rowIndex]; 


        if (theRow.Cells[colIndex].Value.ToString() == "Daily Report") 
        { 
         theRow.DefaultCellStyle.BackColor = Color.LightYellow; 
        } 
        else if (theRow.Cells[colIndex].Value.ToString() == "Monthly Report") 
        { 
         theRow.DefaultCellStyle.BackColor = Color.LightGray; 
        } 
        else if (theRow.Cells[colIndex].Value.ToString() == "SMP Report") 
        { 
         theRow.DefaultCellStyle.BackColor = Color.Snow; 
        } 
        else if (theRow.Cells[colIndex].Value.ToString() == "Weekly Report") 
        { 
         theRow.DefaultCellStyle.BackColor = Color.Pink; 
        } 
        else if (theRow.Cells[colIndex].Value.ToString() == "Hourly Report") 
        { 
         theRow.DefaultCellStyle.BackColor = Color.LightSteelBlue; 
        } 
       } 
      }