Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I want to add a color to the datagridview based on the status. Please take a look at the image below.

ask — IMG[^]

I'm using CellPainting in the datagridview to show a color.

What I have tried:

I tried with my code., but it did not work for me.

1st Try:
if ((e.ColumnIndex == this.dataGridView1.Columns["stat"].Index) && e.Value != null)
                {
                    DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    if (e.Value.Equals("0"))
                    {
                        e.Graphics.FillRectangle(brush, face);
                    }
                    else if (e.Value.Equals("1"))
                    {
                        e.Graphics.FillRectangle(brush1, face);
                    }
                }


2nd Try:
DataGridViewRow row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex];
               if (row.Cells[2].Value != null)
               {
                   if (Int32.TryParse(row.Cells[2].Value.ToString(), out Int32 value))
                   {
                       if (value == 0)
                       {
                           e.Graphics.FillRectangle(brush, face);
                       }
                       else
                       {
                           e.Graphics.FillRectangle(brush1, face);
                       }
                   }
               }

Both of this is not working for me., please help me.
Posted
Updated 13-Aug-24 0:28am
v2
Comments
0x01AA 13-Aug-24 8:22am    
You can do that by CellFormatting- event
E.g. https://www.iditect.com/faq/csharp/changing-datagridview-cell-color-dynamically-in-c.html
PIEBALDconsult 13-Aug-24 9:01am    
Don't use a DataGridView.
ras bry 13-Aug-24 9:26am    
What should I use?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900