[RadGridView] Conditional cell formatting is applied to cells that shouldn't match the formatting criteria

1 Answer 85 Views
GridView
Memo
Top achievements
Rank 1
Iron
Veteran
Memo asked on 01 Sep 2022, 04:44 PM

.NET Framework 4.8

Telerik 2022 R1 

 

There are two conditional cell formats: one for the command cell and another for a checkbox. 

The formatting for the command cell works without issue.  However, for the unavailable column the formatting is applied to other cells and even other columns regardless of the value which is needed. 

Only the first two rows, "1 Giant Slice" and "10inch Small", should have the unavailable cells in red.  These two rows are consistently formatted correctly, but other rows have both unavailable and available cells formatted in red, too.  The rows which have incorrect formatted do not seem to be consistent. 

The debugger and logging the values of the data bound item shows that the proper values are there and only.

e.g.

only the two items and for the proper column are processed by the if statement that applies formatting to the unavailable cell (line 134 in form).
2022-09-01 10:26:08.749  [DEBUG]  Item: 1 Giant Slice  -  Next Available: [01/01/2024 12:30:00]  -  Column Name: Unavailable
2022-09-01 10:26:08.761  [DEBUG]  Item: 10inch Small  -  Next Available: [01/01/2024 12:30:00]  -  Column Name: Unavailable

I've attached a sample project with some sample JSON data that is loaded when the Form Load event is handled. 

 

There has to be something I'm doing wrong but I just can't figure out what it is.

Any help would be most appreciated. TIA

 

Cell formatting event handler:


        private void Dgv_MenuItems_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (_isFiltering)
            {
                return;
            }
            
            if (sender is GridCommandCellElement cellElement)
            {
                var row = dgv_MenuItems.Rows[e.RowIndex];
                var boundObj = (MenuItemAvailabilityModel)row.DataBoundItem;

                //This conditional formatting works without issue
                if (boundObj.CustomAvailability.Count > 0)
                {
                    cellElement.CommandButton.Text = "Change";
                    cellElement.CommandButton.ButtonFillElement.BackColor = Color.LightGreen;
                }
                else
                {
                    cellElement.CommandButton.Text = "Add";
                    cellElement.CommandButton.ButtonFillElement.BackColor = default;
                }
            }
            else if (e.Column.Name == nameof(MenuItemAvailabilityModel.Unavailable))
            {
                var row = dgv_MenuItems.Rows[e.RowIndex];
                var boundObj = (MenuItemAvailabilityModel)row.DataBoundItem;

                //This conditional formatting is applied to cells for which no NextAvailable is set in the data
                if (boundObj.NextAvailableAt.HasValue && boundObj.NextAvailableAt.Value > DateTime.Now)
                {
                    e.CellElement.DrawFill = true;
                    e.CellElement.BackColor = Color.Red;
                    e.CellElement.NumberOfColors = 1;
                }
            }
        }

1 Answer, 1 is accepted

Sort by
1
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Sep 2022, 04:36 AM

Hi, Memo,

Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse), all customization should be reset for the rest of the cell elements. In other words, each "if" condition that applies certain style settings requires the respective "else" statement that resets the applied style settings.

The following help article is quite useful on this topic and it demonstrates how to properly use the CellFormatting event: 

https://docs.telerik.com/devtools/winforms/controls/gridview/cells/formatting-cells#formatting-data-cells 

Feel free to use a similar approach. 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

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Memo
Top achievements
Rank 1
Iron
Veteran
commented on 02 Sep 2022, 04:54 PM

Thank you once again for your help and thorough explanation;  I appreciate it.  Also, thank you for the polite way you told me to RTFM.  ;-)
Tags
GridView
Asked by
Memo
Top achievements
Rank 1
Iron
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or