Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Is it possible to set colour on a specific cell

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Is it possible to set colour on a specific cell
Link Posted: 06-Dec-2005 08:17
Hello!

I just wonder if it's possible to set colour on a specific cell. If it possible have you some pices of sample that show that.

//Tony
Link Posted: 07-Dec-2005 01:34
This sample class shows how to manage and display individual colors for certain column:
[C#]
    internal class CoolNode : Node, IStyledNode
    {
      public class NodeColors
      {
        private Hashtable colors;
        public Color this[Column column]
        {
          get
          {
            return colors == null || !colors.Contains(column) ? Color.Empty : (Color)colors[column];
          }
          set
          {
            if (colors == null) colors = new Hashtable();
            colors[column] = value;
          }
        }
      }
      #region IStyledNode Members
      public CoolNode(NodeBase parent) : base(parent){}
      private static NodeColors foreColors = null;
      public static NodeColors ForeColors
      {
        get
        {
          if (foreColors == null)
            foreColors = new NodeColors();
          return foreColors;
        }
      }
      private static NodeColors bkColors = null;
      public static NodeColors BackColors
      {
        get
        {
          if (bkColors == null)
            bkColors = new NodeColors();
          return bkColors;
        }
      }
      public void OnBeginPaint(CellDrawInfo info)
      {
        if (info.cellSelected)
        {
          info.customForeColor = SystemColors.Info;            
        }
        else
        {
          info.customForeColor = CoolNode.ForeColors[info.currentColumn];
          info.customBackColor = CoolNode.BackColors[info.currentColumn];
        }
      }

      public void OnEndPaint(CellDrawInfo info)
      {
        if (info.customForeColor != Color.Empty)
        { //clear custom info
          info.customForeColor = Color.Empty;
          info.customBackColor = Color.Empty;
        }
      }
      #endregion
    }


This sample shows how to retreive column for certain column via OnGetForeColor/OnGetBackColor methods (that can be overriden by inheritors ):

[C#]
    internal class CoolNodeWithFeedBack : Node, IStyledNode
    {
      #region IStyledNode Members
      public CoolNodeWithFeedBack(NodeBase parent) : base(parent){}
      public void OnBeginPaint(CellDrawInfo info)
      {
        if (info.cellSelected)
        {
          info.customForeColor = SystemColors.Info;            
        }
        else
        {
          info.customForeColor = OnGetForeColor(info.currentColumn);
          info.customBackColor = OnGetBackColor(info.currentColumn);
        }
      }

      public void OnEndPaint(CellDrawInfo info)
      {
        if (info.customForeColor != Color.Empty)
        { //clear custom info
          info.customForeColor = Color.Empty;
          info.customBackColor = Color.Empty;
        }
      }

      protected virtual Color OnGetBackColor(Column col)
      {
        return col.Caption.StartsWith("Green") ? Color.Green : Color.Empty;
      }
      protected virtual Color OnGetForeColor(Column col)
      {
        return col.Caption.StartsWith("Red") ? Color.Red : Color.Empty;
      }
      #endregion
    }