Search code examples
c#databasedatagridviewcellbackcolor

How to change dataGridView cell color on text condition in C#


I want to color the dataGridView cell after it retrieves data from the database. if the cell text has a " X" then color the cell with GreenYellow color. I tried to write the code but it didn't work.

This is the code that I have so far:

    private void button2_Click(object sender, EventArgs e)
    {
        string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand("Select * from TopShineDB.Table1 ;", conDataBase);
        using (MySqlConnection conn = new MySqlConnection(constring))
        {
            try { 
            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdDataBase;
            DataTable dt = new DataTable();
            sda.Fill(dt);

                foreach (DataRow item in dt.Rows)
                {
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = item["Timee"].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = item["CarColorNumber"].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = item["Interior"].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = item["Exterior"].ToString();

                    if (dataGridView1.CurrentCell.Value == item["Interior"] + " X".ToString())
                    {
                        dataGridView1.CurrentCell.Style.BackColor = Color.GreenYellow;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Any ideas how can I make it work?

Thanks


Solution

  • you should set the Style of the cells you want to change.

    If you include the method below where you load the data and into the Scroll event, it will color your cell as desired and only when the cell is visible. This is an important performance issue, if you have many rows

    public void SetRowColor()
    {
        try
        {
            for (int i = 0; i < this.dataGridView.Rows.Count; i++)
            {
                if (this.dataGridView.Rows[i].Displayed)
                {
                    if (this.dataGridView.Columns.Contains("Interior"))
                    {
                        if ((int)this.dataGridView.Rows[i].Cells["Interior"].Value == "X")
                        {
                            this.dataGridView.Rows[i].Cells["Interior"].Style.BackColor = Color.Green;
                            this.dataGridView.Rows[i].Cells["Interior"].Style.ForeColor = Color.White;
                            this.dataGridView.InvalidateRow(i);
                        }
                        else
                        {
                            this.dataGridView.Rows[i].Cells["Interior"].Style.BackColor = Color.White;
                            this.dataGridView.Rows[i].Cells["Interior"].Style.ForeColor = Color.Black;
                            this.dataGridView.InvalidateRow(i);
                        }
                    }
                }
            }
        }
    }
    

    Hope this gets you closer to what you need.

    Thomas