title: How to change dataGridView cell color on text condition in C#
body: <p>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.</p>
  
  <p>This is the code that I have so far:</p>
  
  <pre><code>    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);
              }
  </code></pre>
  
  <p>Any ideas how can I make it work? </p>
  
  <p>Thanks</p>