Datagridview颜色变化

时间:2012-11-02 10:11:20

标签: c# datagrid

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

我应该使用的系统项目还是什么?

如果有人对我正在尝试做的事情有任何其他选择,那也将不胜感激。

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

3 个答案:

答案 0 :(得分:1)

使用以下命名空间:

using System.Drawing;

希望这将有所帮助。

答案 1 :(得分:0)

您好你可以找到你的答案Here

答案 2 :(得分: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;
                    }
                }
            }