cell styles not working in datagridview

ankit goel 766 Reputation points
2023-10-26T10:01:34.1433333+00:00

Hi experts ,

i had used some of the code from the user here in microsoft q and A . and tried to modified it according to my requirement

  InitializeComponent();

            this.EnableHeadersVisualStyles = false;
            this.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9.75F, FontStyle.Bold); // HEADER FONT BOLD 
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // fill the blank area 
            
            this.BackgroundColor = Color.FromArgb(250, 243, 198); // extra area color 
            this.DefaultCellStyle.SelectionBackColor = Color.FromArgb(168, 201, 170); // full row select color to green
            this.RowsDefaultCellStyle.BackColor = Color.FromArgb(250, 243, 198);
// rows back color 
            this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(250, 243, 198); // change back color of header cells


  protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            // only place where it is working 
            e.CellStyle.BackColor = Color.FromArgb(0, 0, 0); // change cell color to black 
            e.CellStyle.ForeColor = Color.FromArgb(255, 255, 255); // change cell color to white  
            Font currentFont = e.CellStyle.Font;
            Font newFont = new Font(currentFont.FontFamily, 11, currentFont.Style | FontStyle.Bold);
            e.CellStyle.Font = newFont;

           
            base.OnEditingControlShowing(e);
        }


using the above , it gives me as i expected

User's image

but when i leaves the cell , it maintains its forecolor (for the full same row ), even though i applied to code to revert back but not maintaining its bold font .
User's image

protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
        {
                      
            this.CurrentCell.Style.ForeColor = Color.FromArgb(0, 0, 0);
}

but surprisingly reverts back when i leave the row
User's image

so its clear that cell leave has no effect .

I am here trying to set the forecolor of the cell back to back as soon as the editing is finished for the current cell (whichit is only changing when i changed the row .)
another thing is font issue which i want is for all the cells in all the rows and columns

i had tried multiple methods to like cell end edit , cell leave but none of them is working . Pls help in this matter .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,908 questions
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.
11,123 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 47,986 Reputation points Microsoft Vendor
    2023-10-30T08:42:14.07+00:00

    Hi @ankit goel , Welcome to Microsoft Q&A,

    I used your code and it didn't appear that the current grid cannot be modified. To make all cells bold just use the OnCellFormatting event. The main code examples are as follows:

    using System.Drawing;
    using System.Windows.Forms;
    
    namespace _10_30_x
    {
        public partial class CustomControl2 : DataGridView
        {
            public CustomControl2()
            {
                InitializeComponent();
                this.EnableHeadersVisualStyles = false;
                this.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9.75F, FontStyle.Bold); // HEADER FONT BOLD 
                this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // fill the blank area 
    
                this.BackgroundColor = Color.FromArgb(250, 243, 198); // extra area color 
                this.DefaultCellStyle.SelectionBackColor = Color.FromArgb(168, 201, 170); // full row select color to green
                this.RowsDefaultCellStyle.BackColor = Color.FromArgb(250, 243, 198);
                // rows back color 
                this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(250, 243, 198); // change back color of header cells
            }
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
            protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
            {
                base.OnCellFormatting(e);
    
                if (e.Value != null && e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    Font newFont = new Font(e.CellStyle.Font.FontFamily, 11, e.CellStyle.Font.Style | FontStyle.Bold);
                    e.CellStyle.Font = newFont;
                }
            }
            protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
            {
                // only place where it is working 
                e.CellStyle.BackColor = Color.FromArgb(0, 0, 0); // change cell color to black 
                e.CellStyle.ForeColor = Color.FromArgb(255, 255, 255); // change cell color to white             
                base.OnEditingControlShowing(e);
            }
        }
    }
    
    
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly 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.


0 additional answers

Sort by: Most helpful

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.