change style of a single cell in a datagrid

Nov 7, 2008 at 3:05 PM
How could you change, in your code, the style of a single cell?
For example the background colour of a cell.
Coordinator
Nov 7, 2008 at 4:17 PM
How about this:

            Style style = new Style(typeof(DataGridCell), DataGrid_Standard.CellStyle);
            Setter setter = new Setter(DataGridCell.BackgroundProperty, Brushes.LightGreen);
            style.Setters.Add(setter);
            Helper.GetCell(DataGrid_Standard, 0, 3).Style = style;
Nov 7, 2008 at 9:25 PM
This last line seems to be using an unexisting class and an unexisting method.
Helper.GetCell(DataGrid_Standard, 0, 3).Style = style;

This all look very good but what/where is a Helper?
There is all kind of helper functions but they all seem to be private.

I also do not see anything which returns a DataGridCell.

I cannot find  any GetCell function either

Also I gather the cell maybe virtualize which I do not currently understand but I will read what I can find about this.

Andre
Coordinator
Nov 7, 2008 at 10:23 PM
It is actually a custom implementation.  See this thread, http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=34065.
Nov 8, 2008 at 2:24 PM

There is some problems with cell style

Below is a simplified version, used in this example, with virtualization removed.
I have an Datagrid named  SourceProgram with 100 rows and 4 columns
I execute the following ( note: i == 0 ) 
    
    Style
style = new Style(typeof(DataGridCell), SourceProgram.CellStyle);
    Setter setter = new Setter(DataGridCell.BackgroundProperty, Brushes.LightGreen);
    style.Setters.Add(setter);
    DataGridCell cell = GetCell(i, 2);
    if (cell != null)
        cell.Style = style;

 

After this code is executed -----------------
row 0 cell 2 is coloured and so are
row 29 cell 2
row 63 cell 2
row 97 cell 2 seen by scrolling (This vary depending on the size of the grid)

When I scroll back row 0 cell 2 is back to the default or maybe coloured
the others seem to stay coloured.

--------------------------------------------------------------------------------------------------------code
       public 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)
                {
                    break;
                }
            }
            return child;
        }

        public DataGridCell GetCell(int row, int column)
        {
            DataGridRow rowContainer = GetRow(row);

            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                DataGridCell cell = null;

                // try to get the cell but it may possibly be virtualized
                try
                {
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                catch (System.Exception)
                {
                }
                return cell;
            }
            return null;
        }

        public DataGridRow GetRow(int index)
        {
            DataGridRow row = (DataGridRow)SourceProgram.ItemContainerGenerator.ContainerFromIndex(index);
            return row;
        }

Nov 8, 2008 at 3:28 PM
If virtualization is enable there is an obvious problem with
        
        public DataGridRow GetRow(int index)
        {
            DataGridRow row = (DataGridRow)SourceProgram.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                // may be virtualized, bring into view and try again
                SourceProgram.ScrollIntoView(SourceProgram.Items[index]);
                row = (DataGridRow)SourceProgram.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }


the datagrid scroll unexpectedly. Virtualization  is extremely hard to do.

Andre
Aug 27, 2009 at 11:20 AM

Hi this is sarath chandra.

I have same requirement to apply background color cell in wpf datagrid. I done by using above code. Its working very fine untill i scroll down the grid. When scroll down the cell values are changing.

Can u give solution for this.

 

 

Aug 28, 2012 at 11:49 AM

Ok..this is straightforward solution and  it works until your DataGrid updated. ( for e.g. if you have treeview-like column and you try to expand/collapse nodes) 

the style is dropped to Column.CellStyle. If you know a general technique for solving this issue it would be really great!! =)