2012-11-02 5 views
0

J'essaie de changer la couleur en fonction de la valeur dans la cellule, complète ou incomplète, mais pour une raison quelconque, il est dit que «Couleur» n'existe pas dans le contexte actuel.Datagridview changement de couleur

Y a-t-il un élément système que je devrais utiliser ou quelque chose?

Si quelqu'un a des alternatives à ce que j'essaie de faire cela serait également apprécié.

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; 
      } 
     } 

Répondre

1

utiliser l'espace de noms ci-dessous:

using System.Drawing;

espère que cela aidera.

0

Salut Vous pouvez trouver votre answere Here

0

J'ai utilisé ce dans un projet un certain temps: -

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; 
        } 
       } 
      }