Search code examples
c#winformsdatagridviewfonts

Bold font in specific dataGridView rows


I am looking for and trying different solutions to my problem, but none of them work. I need to set bold font in all cells in a row if cell with specific index has specific value. What am I doing wrong?

foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (row.Cells[14].Value.ToString() == "Yes")
                        {
                            row.DefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
                        }
                    }

EDIT: Sorry, it was my fault. All time the conditional statement checked the wrong cell and my code works perfectly.


Solution

  • So if a DataGridViewCell has a certain value, you want that this value is printed in a certain font.

    The font that is used to show the value of a DataGridViewCell is in the DataGridViewCellStyle of the cell.

    So we can make your requirement more generic: if a DataGridViewCell has a certain value, you want that the cell uses a certain DataGridViewCellStyle to show its value.

    There are two methods that you can use:

    • Whenever the cell changes value, and if it has got the "certain value", give this cell the special Style, whenever it changes to another value, let it use the default style.
    • Whenever the value of the cell has to be formatted, format the cell.

    To me the first method seems the most easy one. Most cells won't change values very often, so it seems to me that it is more efficient to do this only when the value changes, and not every time the value of the cell has to be formatted.

    About DataGridViewCellStyles

    A DataGridViewCell can (or cannot) have a DataGridViewCellStyle. If the cell has a style, it uses this Style. If it does not have a style (value equals null), it uses the DefaultCellStyle of the DataGridViewColumn. If the column also does not have a style, the DataGridView.DefaultCellStyle is used.

    To let a Cell or a Column use the cell style of the parent, just make the (Default)CellStyle null. The actual used style, after checking the CellStyle, the column's cell style etc is in DataGridViewCell.InheritedStyle

    So if you want that a certain cell uses a non-default DataGridViewCellStyle, just assign the cell style that you want. If you want to use the default cell style for that column again, just assign null.

    Change the Cell style whenever the value changes

    const string specialDisplayValue = "yes";
    IEqualityComparer<string> CellTextcomparer => StringComparer.CurrentCultureIgnoreCase;
    
    void OnCellValueChanged(DataGridViewCell cell)
    {
        string displayedCellValue = this.GetDisplayedValue(cell);
        if (this.CellTextComparer.Equals(displayedCellValue, specialDisplayValue))
        {
             // special text shown, use special style with special font:
             cell.Style = new DataGridViewCellStyle(cell.Column.InheritedStyle);
             cell.Style.Font = new Font(cell.Style.Font, FontStyle.Bold);
        }
        else
        {
             // other value, use the default style:
             cell.Style = null; // the style from the column will be used.
        }
    }
    

    If the InheritedStyles of the Columns won't change, consider to define the special style only once in the constructor. Then you only have to decide whether to assign null, or the special style. If all columns use the same style, you only have to use one special style, created with the DataGridView.CellStyle as template.

    Format the cell yourself

    Use the event:

    dataGridView1.CellFormatting += dataGridView1_CellFormatting;
    
    private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        string displayValue = e.Value.ToString();
        if (this.CellTextComparer.Equals(displayValue, specialDisplayValue))
        {
            // special value, special font
            e.CellStyle.Font = new Fone(e.CellStyle.Font, FontStyle.Bold);
        }
        // else: different value, don't change the font
    }
    

    Note: this method will be called every time that the cell must be formatted, even if the value of the cell has not been changed. Therefore it is less efficient.