Add Backcolors dependent on values using dataGridView1_RowsAdded

Andreas ss 726 Reputation points
2022-02-06T17:45:37.53+00:00

Hello!

I am creating a datagridview by first populate a DataTable with values.
After I have populated the DataTable, I add that to the datagridview like this: dataGridView1.DataSource = dt

Now is my idéa to add a backcolor dependent on different values using: dataGridView1_RowsAdded. I am looking for this event as I beleive that is the fastest
way to add colors to the cell. (Using a for loop after the gridview has been created I can do but this I think is not as effecient)
(If it is possible to add a background color to the DataTable cell in the first place is possible, but are not sure that is possible and automaticall will show in the datagridview?)

  1. Add a backcolor to the cells in the datagridview that contains: "hello1" and "hello3" in Column index 0
  2. Add a backcolor to the cells in the datagridview that contains: "world2" and "world4" in Column index 1

But there is only 1 cell out of 4 that is painted with a Green color which is "hello1".

I am not sure what I am missing?

Thank you!

        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(createdatagridview).Start();
        }
        void createdatagridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("column 0"); dt.Columns.Add("column 1");
            for (int i = 0; i < 5; i++)
            {
                dt.Rows.Add();
                dt.Rows[i][0] = "hello" + i;
                dt.Rows[i][1] = "world" + i;
            }
            Invoke((MethodInvoker)delegate { dataGridView1.Columns.Clear(); dataGridView1.DataSource = dt; });
        }
        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (dataGridView1.Columns.Count >= 1)
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)
                {
                    if (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "hello1" ||
                        dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "hello3") { dataGridView1.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.DarkSeaGreen; }
                }
            }
            if (dataGridView1.Columns.Count >= 2)
            {
                if (dataGridView1.Rows[e.RowIndex].Cells[1].Value != null)
                {
                    if (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "world2" ||
                        dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString() == "world4") { dataGridView1.Rows[e.RowIndex].Cells[1].Style.BackColor = Color.DarkSeaGreen; }
                }
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2022-02-06T19:02:16.773+00:00

    The following does not care how the DataGridView was loaded. Note I used case insensitive. For other columns follow the same logic.

    namespace DataGridViewGetCellStyle  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
      
                dataGridView1.CellFormatting += DataGridView1OnCellFormatting;  
            }  
      
            private readonly List<string> _firstNamesList = new List<string>() { "anne", "mary" };  
            private void DataGridView1OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
            {  
                if (e.ColumnIndex != 0) return;  
      
                if (_firstNamesList.Contains(Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[0].Value), StringComparer.OrdinalIgnoreCase))  
                {  
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;  
                    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);  
                }  
                else  
                {  
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Empty;  
                    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Regular);  
                }  
            }         
      
        }  
    }  
      
    

    171690-figure1.png


0 additional answers

Sort by: Most helpful