cell styles not working [part -2]

ankit goel 746 Reputation points
2023-11-04T03:53:51.9366667+00:00

Hi ,

 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
         this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  

  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  


            Font currentFont = e.CellStyle.Font;
            Font newFont = new Font(currentFont.FontFamily, 11, currentFont.Style | FontStyle.Bold);
            e.CellStyle.Font = newFont;

           
            base.OnEditingControlShowing(e);
        }

  protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
        {

            base.OnCellEndEdit(e);

          

            this.CurrentCell.Style.ForeColor = Color.FromArgb(0, 0, 0);


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

        }

The above is the code i am using to create cell styles in datagridview . The only problem left in this , is that the white forecolor which gets set during the oneditingcontrolshowing cannot gets changed untill the row gets changed . i tried changing it in the oncellendedit and even in cell leave but it doesn't change . Can you please suggest why the white forecolor don't gets changed untill focus leaves the current row .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,738 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.
9,478 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 13,506 Reputation points Microsoft Vendor
    2023-11-06T09:51:32.8933333+00:00

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

    After unremitting efforts, I seem to have found the goal.

    Give this property a try.

    this.DefaultCellStyle.SelectionForeColor = Color.Black; //Set the text color of the selected cell to black
    

    User's image

    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