Click here to Skip to main content
11,797,944 members (77,613 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
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 at 29-May-13 10:02am
Please help me with the problem I have. Your link is almost completely irrelevant.
Kenneth Haugland at 29-May-13 10:29am
I dont actually think so, with a IValueConverter I dont think your problem will occur.
rohith naik at 30-May-13 7:07am
Can you atleast tell me why you think using a IValueConverter will help my problem ?
rohith naik at 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
0 CPallini 575
1 Maciej Los 375
2 OriginalGriff 353
3 Abhinav S 342
4 Richard MacCutchan 218
0 OriginalGriff 2,150
1 Maciej Los 1,790
2 KrunalRohit 1,456
3 CPallini 1,415
4 Richard MacCutchan 1,016

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2015
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