Click here to Skip to main content
14,690,541 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have loaded the data from database to datagridview, then i added new Column "Check-Out" just when the application execute , this the code of new column :



I want color the cell which the gender "M" by ForestGreen and the gender "F" with the color Red


But i get all of the color ForestGreen !!! however i have some gender "F" !!!

How can i fix mycode ?

Thanks,

What I have tried:

Code add column 'Check Out':

DataGridViewButtonColumn column = new DataGridViewButtonColumn();
        dataGridView1.Columns.Add(column);
        column.FlatStyle = FlatStyle.System;
        column.Name = "Check-Out";
        column.HeaderText = "Check-Out";
        column.Text = "Check-Out";
        column.UseColumnTextForButtonValue = true;



This is my code :

using (checkinentrepriseEntities2 context = new checkinentrepriseEntities2())
        {            
            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                try
                {
                    clients clienTTT = context.clients.FirstOrDefault(i => i.gender == "M");
                    if (clienTTT != null)
                    {
                        column.DefaultCellStyle.ForeColor = Color.ForestGreen;
 
                    }
                    else
                    {
                        column.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
            }
Posted
Updated 1-Nov-16 0:44am

1 solution

Try setting the cell style not the column :
DataGridViewCell.Style Property (System.Windows.Forms)[^]
datagridView1[1,1].Style.ForeColor = Color.ForestGreen;
   

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