EF: Change cell color based on Value

Gerald Oakham 101 Reputation points
2022-08-02T17:49:46.48+00:00

Hi,m

I am trying to teach myself EF by using a practical example.
I am accessing a System_users table in a DB, and I can now get that to display, and then only the columns I am interested in (don't sound like a mean feet, but it is for me ).
I would now like to change the font colour of a cell if it contains a keyword, ie: if the user has not changed their password from the default value.

I have added a counter, so I can utilize this to select the row, but when I run it nothing changes ( but the counter is correct )
Please can you advise when I need to do ?

            dataGridView1.DataSource = objet.system_users.OrderBy(x=>x.user_id).ToList();  
            //                                            ^DataSource  ^Table                        ^columnToOrderBy  
  
            int 123Counter = 0;  
            //MessageBox.Show(dataGridView1.Columns[2].Name);  
         
            foreach (DataGridViewRow item in dataGridView1.Rows)  
            {  
                if (item.Cells["userpasswordDataGridViewTextBoxColumn"].Value.ToString().Contains("123"))  
                {  
                    23Counter++;  
                    //dataGridView1.CurrentCell.Style = new DataGridViewCellStyle { ForeColor=Color.Red};  
                    //dataGridView1.SelectedCells[0].Style = new DataGridViewCellStyle { ForeColor = Color.Red };  
                    //item.Cells["userpasswordDataGridViewTextBoxColumn"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };  
                    //this.dataGridView1.CurrentCell.Style.ForeColor = Color.Red;  
                    dataGridView1.Rows[123Counter].Cells[2].Style = new DataGridViewCellStyle { ForeColor = Color.Red };  
                }  
            }  
  
            label1.Text="People with default passwords " + 123Counter.ToString();  

thank you all

Developer technologies .NET Other
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-08-03T02:37:45.727+00:00

    @Gerald Oakham , Welcome to Micrososoft Q&A, based on my test, I almost reproduced your problem.

    Please make the following changes:

    First, Please don't use the varibale that starts with the number, it is Irregular. Please try to use Counter to replace it.

    Second, Please add one to the counter after you color the current row.

    Here is a code example you could refer to.

    private void button1_Click(object sender, EventArgs e)  
            {  
                int Counter = 0;  
      
                foreach (DataGridViewRow item in dataGridView1.Rows)  
                {  
                    if (item.Cells["user_password"].Value.ToString().Contains("123"))  
                    {  
                        dataGridView1.Rows[Counter].Cells[1].Style = new DataGridViewCellStyle { ForeColor = Color.Red };  
                        Counter++;  
                    }  
                }  
      
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                TestEntities context=new TestEntities();  
                dataGridView1.DataSource = context.tbl_User.OrderBy(x => x.user_id).ToList();  
      
            }  
    

    If you click the button, you will see the related cells is colored.

    227451-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Gerald Oakham 101 Reputation points
    2022-08-03T19:22:37.833+00:00

    Jack:, YOU. ARE. A. STAR.

    thank you :-)

    would you know how to check the length of a field also?

    I have tried the following, but I get "input string was not in a correct format".

                Counter = 0;  
                foreach (DataGridViewRow item in dataGridView1.Rows)  
                {  
                    int  a = Convert.ToInt32(item.Cells["userpasswordDataGridViewTextBoxColumn"].Value);  
                    if (a < 4)  
                    {  
                       MessageBox.Show(item.Cells["userpasswordDataGridViewTextBoxColumn"].Value.ToString());  
                        Counter++;  
                    }  
               
                }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.