Wizard
Wizard

Reputation: 1152

Datagridview colour changing

I'm trying to change the colour based on the value in the cell, complete or incomplete but for some reason it's saying that 'Color' doesn't exist in the current context.

Is there a system item I should be using or something?

If anyone has any alternatives to what I'm trying to do that would also be appreciated.

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

Upvotes: 0

Views: 105

Answers (3)

Derek
Derek

Reputation: 8630

I used this in a project a while back :-

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

Upvotes: 0

Rohit Vyas
Rohit Vyas

Reputation: 1959

Hi You can found your answere Here

Upvotes: 0

Tapas Mahata
Tapas Mahata

Reputation: 351

use the below namespace:

using System.Drawing;

hope thiS will help.

Upvotes: 1

Related Questions