WPF: Telerik RadGridView C# RowLoaded/CellLoaded

1 Answer 184 Views
Designs, skins, themes Forum suggestions
Jackson
Top achievements
Rank 1
Jackson asked on 09 Aug 2023, 10:40 PM | edited on 09 Aug 2023, 10:52 PM

Currently I have the below code that works when the "Notes" Column is the last column and is on screen.

If grid overflows and needs a scroll bar the CellLoaded method gets called indefinitely until user scrolls to make the "Notes" Column show on the screen. And needs to stay on screen while scrolling up/down to keep expected functionality.


private void radGridView_CellLoaded(object sender, CellEventArgs e)
{
    GridViewCellBase cellItem = e.Cell;
    // Only run once per row, after all cells are loaded [Notes is last loaded column]
    if (cellItem.Column.UniqueName == "Notes")
    {
        GridViewRowItem rowItem = cellItem.ParentRow;
        if (rowItem.Item != null && rowItem.Item is DataRowView)
        {
            _viewModel.ToggleHighlightingCells_BasedOn_CellSpecificHighlighting_Column(((DataRowView)rowItem.Item).Row, rowItem.Cells);
        }
    }
}

 

I have tried to use RowLoaded (see below), but the styling achieved is not applied correctly

private void radGridView_RowLoaded(object sender, RowLoadedEventArgs e)
{

    if (e.Row is GridViewRowItem rowItem && rowItem.Item is DataRowView dataRowView)
    {
        _viewModel.ToggleHighlightingCells_BasedOn_CellSpecificHighlighting_Column(dataRowView.Row, rowItem.Cells);
    }
}

 

 

Any thoughts of what I can do to make the RowLoaded method work like the CellLoaded?

Is Loaded methods ran first is there a trigger that happens after Load?

I am needing the data in the row to be populated and then the styling applied after, based on the data loaded in the row

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 10 Aug 2023, 11:39 AM

Hello Jackson,

Can you send us the code of  ToggleHighlightingCells_BasedOn_CellSpecificHighlighting_Column method as well? This will allow us to properly investigate this and provide a solution. 

In general such styling can be achieved using a style selector. More information is avaible here: WPF DataGrid - CellStyleSelector.

I am looking forward to your reply.

Regards,
Dimitar
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.

Jackson
Top achievements
Rank 1
commented on 10 Aug 2023, 02:18 PM

This is for a more complex highlighting

pressing a button on the screen can make any where from 1 cell to multiple rows need to be highlighted. Stay highlighted when scrolling/rerendering, but upon clicking/focusing on any cell in grid those highlighting is removed

 

public void ToggleHighlightingCells_BasedOn_CellSpecificHighlighting_Column(DataRow row, IList<GridViewCellBase> cells)
{
    var cellsToHighlight = row["CellSpecificHighlighting"].ToString().Split(';').ToList();
    cells.Where(c => c.Column.UniqueName != "RowNum").ToList().ForEach(cell =>
    {
        var highlightColor = Color.FromArgb(100, 162, 60, 69);
        var currColor = ((SolidColorBrush)cell.Background).Color;
        var shouldHighlight = cellsToHighlight.Contains(cell.Column.UniqueName);
        var shouldUnhighlight = cellsToHighlight.Contains("Unhighlight:" + cell.Column.UniqueName);
        if (shouldHighlight)
        {
            cell.Background = new SolidColorBrush(Color.Add(highlightColor, currColor));
        }
        else if (shouldUnhighlight)
        {
            cell.Background = new SolidColorBrush(Color.Subtract(currColor, highlightColor));

            // Only unhighlight on first render of row (rerender doesn't have highlight to remove)
            cellsToHighlight.Remove("Unhighlight:" + cell.Column.UniqueName);
        }

    });

    row["CellSpecificHighlighting"] = string.Join(";", cellsToHighlight);
}


Jackson
Top achievements
Rank 1
commented on 10 Aug 2023, 02:30 PM

this CellLoaded logic is for when scrolling/rerendering to add back highlighting if needed. But the CellLoaded keeps getting called if there is overflow for the grid. Also for every time scrolling left and right. Basically rows and cells are killed/reused if off screen?

Jackson
Top achievements
Rank 1
commented on 11 Aug 2023, 03:14 PM

Let me know if that doesn't make sense 👍
Dimitar
Telerik team
commented on 14 Aug 2023, 11:40 AM

Hi Jackson, 

I believe that this is expected because of the virtualization. These events are not suitable for styling the elements. I would recommend implementing the approach from this article: WPF DataGrid - CellStyleSelector.

Let me know if you have additional questions.

Tags
Designs, skins, themes Forum suggestions
Asked by
Jackson
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or