C# Datagridview颜色更改

C# Datagridview颜色更改,c#,datagrid,C#,Datagrid,我试图根据单元格中的值更改颜色,完整或不完整,但出于某种原因,它表示“颜色”在当前上下文中不存在 是否有我应该使用的系统项或其他东西 如果有人有任何替代我尝试做的事情,我也会很感激 foreach (DataGridViewRow row in dtaFinished.Rows) { string RowType = row.Cells[4].Value.ToString(); if (RowType == "Completed"

我试图根据单元格中的值更改颜色,完整或不完整,但出于某种原因,它表示“颜色”在当前上下文中不存在

是否有我应该使用的系统项或其他东西

如果有人有任何替代我尝试做的事情,我也会很感激

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

使用以下命名空间:

使用系统图


希望这会有所帮助。

您好,您可以找到您的Answare

我不久前在一个项目中使用了这个:-

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