Click here to Skip to main content
15,889,281 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':

C#
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 :

C#
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 31-Oct-16 23:44pm

1 solution

Try setting the cell style not the column :
DataGridViewCell.Style Property (System.Windows.Forms)[^]
C#
datagridView1[1,1].Style.ForeColor = Color.ForestGreen;
 
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