Code to make cell bold stopped working

1 Answer 23 Views
GridView
Michael
Top achievements
Rank 1
Veteran
Michael asked on 25 May 2022, 07:44 AM

Hello.

I have the following code that used to work:

private void cellFormatting(object sender, CellFormattingEventArgs e)
{
if (chkHighlight.Checked && e.RowIndex < grid.RowCount - 1 &&
e.Column.FieldName != nameof(Audited.ValidFromTime) &&
e.CellElement.Value?.Equals(grid.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value) != true)
e.CellElement.Font = new Font(e.CellElement.Font, FontStyle.Bold);
else
e.CellElement.ResetValue(LightVisualElement.FontProperty, ValueResetFlags.Local);
}

For some reason none of the cells comes up bold anymore (I set a breakpoint to make sure the code that sets the Font was being executed).

Any ideas?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 May 2022, 01:33 PM

Hello, Michael,

Please have in mind that the font creating is a time-consuming operation. Hence, the correct approach is to create a global variable for the font which to be used in the CellFormatting event: 

        Font f = new Font("Arial", 10f, FontStyle.Bold);

        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.Column.Name == "UnitPrice" && e.CellElement.Value != null && (decimal)e.CellElement.Value > 20)
            {
                e.CellElement.Font = f;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.FontProperty, ValueResetFlags.Local);
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or