Skip to main content Skip to footer

Avoid Overlapping Cell/Row Presenter Background in C1DataGrid

C1DataGrid for SilverLight provides lot of options to customize the Cell or Row contents. One such Option is the availability of LoadedCellPresenter event and LoadedRowPresenter event. As the name suggests, these two events provides the flexibility to customize the Cell or Row respectively. With the help of the Presenter object, you can control the Visual properties like Background, Foreground, Cell contents and many other things conditionally.


private void \_DataGrid\_LoadedCellPresenter(object sender, DataGridCellEventArgs e)  
{  
    DataGridCell cell = e.Cell;  

    if (cell.Presenter != null && cell.Column.DisplayIndex = = 8 )  
    {  
        if (cell.Row.Index % 2 == 0)  
        {  
            cell.Presenter.Background = null;  
            cell.Presenter.Background = new SolidColorBrush(Colors.Red);  
            cell = null;  
        }  
    }  
}  

The above code snippet changes the Cell background for alternative rows in the 9th Column of the C1DataGrid. However, with this flexibility comes a small limitation in the control. If the grid is in scrolling mode, and you start scrolling the grid, you would observe that the Cell foreground gets applied on those cells where it was not applied initially. Similar behavior can be observed with Row Background where the row back color starts overlapping with other rows when the grid is scrolled Vertically. Now most of you may consider this as a bug; which is actually not. This behavior happens due to the fact that C1DataGrid cell presenters are reused when the Viewport changes (scrolling in this scenario). So if any changes are not being done on the new loading cells, reused presenter object retains its modified appearance from the last cell. Simple and easy solution to avoid this problem is to reset the settings done in the LoadedCellPresenter/LoadedRowPresenter event, in the UnloadedCellPresenter/UnloadedRowPresenter event.


void \_DataGrid\_UnloadedCellPresenter(object sender, DataGridCellEventArgs e)  
{  
   e.Cell.Presenter.Foreground = _DataGrid.Foreground ;  
}  

Download the attached samples for complete implementation. To observe the original behavior, you would need to comment out the Event Handler statements for UnloadedCellPresenter and UnloadedRowPresenter events. Download C# Sample Download VB.Net Sample

MESCIUS inc.

comments powered by Disqus