Click here to Skip to main content
13,088,101 members (69,974 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am using the below method to highlight cells from my code-behind for my data grid. But the problem is that when I scroll out of view, the highlighted cells are getting lost and are jumping around. I have set both row and column virtualization to false. please help.

public void ColourCell(int row, int column, Color color, bool HighlightFlag = true)
            DataGridRow rowContainer = GetRow(row);
            if (rowContainer != null)
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                // try to get the cell but it may possibly be virtualized but enable virtualizion is off
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                // now try to bring into view and retreive the cell
                if (cell != null)
                    if (HighlightFlag == false)
                        cell.Background = new SolidColorBrush(Colors.MediumPurple);
                        cell.Background = new SolidColorBrush(color);

public DataGridRow GetRow(int index)
            DataGridRow row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
                row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
            return row;

static T GetVisualChild<T>(Visual parent) where T : Visual
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                    child = GetVisualChild<T>(v);
                if (child != null)
            return child;
Posted 29-May-13 3:12am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

rohith naik 29-May-13 10:02am
Please help me with the problem I have. Your link is almost completely irrelevant.
Kenneth Haugland 29-May-13 10:29am
I dont actually think so, with a IValueConverter I dont think your problem will occur.
rohith naik 30-May-13 7:07am
Can you atleast tell me why you think using a IValueConverter will help my problem ?
rohith naik 30-May-13 7:34am
Additionally, I am not using XAML as there is a very complex logic to which cells I want to highlight. This works fine if the datagrid fits into 'one page' of sorts, i.e, there is no need to scroll. But if i scroll down and lose sight of the cell, The background change which I made jumps around.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100