Home - Forums-.NET - FlyGrid.Net (Windows Forms) - A question about PaintCell, PaintCellValue, PaintTextInCell

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

A question about PaintCell, PaintCellValue, PaintTextInCell
Link Posted: 03-May-2006 04:26
Hello!

I find it difficult to know which of these three methods to use PaintCell, PaintCellValue and PaintTextInCell.

I have read the documentation about this but it doesn't say very much.

Can somebody explain something what is the differens about these three methods

//Tony
Link Posted: 03-May-2006 15:20
PainCell is a root of cell paint procedure, you can override this if you need to completely manage cell drawing or add some non-text drawings.
PaintCellValue - next level, as you know values in the cell can be non text, if cell paints images or checkboxes. You can override this procedure when you need to draw abstract value of cell - image, checkbox, button...
PaintTextInCell - draw text values in the cell, you can override this procedure if you need to override standard text drawing.
Link Posted: 03-May-2006 19:22
Thanks for your answer.

If you want to add a drop down symbol to a cell will PaintCell be the suitable method to put code in to handle that.

If you want to change customBackColor for a cell to some Color what method of PaintCell,PaintCellValue or PaintTextInCell will be most appropriate.

If you want to change color on text what method will then be most suitable?

//Tony
Link Posted: 23-Jun-2006 00:42
[quote="NineRays"]PainCell is a root of cell paint procedure, you can override this if you need to completely manage cell drawing or add some non-text drawings.
PaintCellValue - next level, as you know values in the cell can be non text, if cell paints images or checkboxes. You can override this procedure when you need to draw abstract value of cell - image, checkbox, button...
PaintTextInCell - draw text values in the cell, you can override this procedure if you need to override standard text drawing.



Are there any examples of these overrides?
I'm interested in using FlyGrid but need to custom draw each cell so that the text in each cell shows two lines of text (two different values).  I also would like to custom draw the cell so it looks like a basic raised button.
Link Posted: 23-Jun-2006 02:30
[VB.Net]
Public Class ColumnWithImage
Inherits Column

'this method overrides GetEditableCellRect to return modified rectangle if image is displayed next to the cell's text
Public Overloads Overrides Function GetEditableCellRect(ByVal location As System.Drawing.Point, ByVal context As NineRays.Windows.Forms.Grids.FlyGridViewPort, ByVal grid As NineRays.Windows.Forms.Grids.FlyGridViewPort, ByVal node As NineRays.Windows.Forms.Data.NodeBase) As System.Drawing.Rectangle
  Dim r As System.Drawing.Rectangle = MyBase.GetEditableCellRect(location, context, grid, node)
  If (Not Me.Icon Is Nothing) Then
    Dim iconWidth As Integer = Me.Icon.Width + 2
    r.X = r.X + iconWidth
    r.Width = r.Width - iconWidth
  End If
  Return r
End Function

Private _icon As System.Drawing.Icon = Nothing

'this property returns an icon object for displaying in the cell
Public ReadOnly Property Icon() As System.Drawing.Icon
  Get
    If (_icon Is Nothing) Then
      ' this icon is added to the current project as Embedded Resource
      'load icon from the managed resources of current assembly
      Dim icoStream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(\"FlyGridDemo.icon.ico\")
      If (Not icoStream Is Nothing) Then
        _icon = New System.Drawing.Icon(icoStream)
      End If
    End If
    Return _icon
  End Get
End Property

'this method overrides PaintCellValue method to display image in the cell
Public Overrides Sub PaintCellValue(ByVal dci As CellDrawInfo)
  If (Not Me.Icon Is Nothing) Then
    dci.g.DrawIcon(Me.Icon, dci.cellRect.X, dci.cellRect.Y)
    dci.cellRect.X = dci.cellRect.X + Me.Icon.Width
    dci.cellRect.Width = dci.cellRect.Width - Me.Icon.Width
  End If
  MyBase.PaintCellValue(dci)
End Sub
End Class
Link Posted: 23-Jun-2006 02:31
[c#]
public class CurrencyColumn : NumberColumn
{
  public override EditorStyle EditorStyle
  {
    get
    {
      return EditorStyle.Spin;
    }  
  }

  public CurrencyColumn(string name) : base(name){}        
  private Color negativeValueColor = Color.Red;
  public Color NegativeValueColor
  {
    get
    {
      return negativeValueColor;
    }
    set
    {
      if (negativeValueColor != value)
      {
        negativeValueColor = value;
        base.OnChanged(NineRays.Windows.Forms.Grids.InvalidationMode.ColumnWithoutHeader, false);
      }
    }
  }

  private Color zeroValueColor = Color.ForestGreen;
  public Color ZeroValueColor
  {
    get
    {
      return zeroValueColor;
    }
    set
    {
      if (zeroValueColor != value)
      {
        zeroValueColor = value;
        base.OnChanged(NineRays.Windows.Forms.Grids.InvalidationMode.ColumnWithoutHeader, false);
      }
    }
  }
  
  protected override void PaintTextInCell(CellDrawInfo dci, Brush bkBrush, Color foreColor)
  {
    IConvertible value = this.GetValue(dci.node) as IConvertible;
    if (value != null)
    {
      double dblValue = value.ToDouble(System.Globalization.CultureInfo.InvariantCulture);
      if (dblValue == 0)
        foreColor = this.ZeroValueColor;
      else if (dblValue > 0)
        foreColor = this.NegativeValueColor;
    }
    base.PaintTextInCell (dci, bkBrush, foreColor);
  }
}
Link Posted: 25-Jun-2006 23:47
Thanks for the samples, but neither meet my requirement of displaying two values in the one cell.  Such as:


------ ------
| 3.45 | 3.55 |
| $100 | $186 |
------ ------
Link Posted: 26-Jun-2006 01:14
See the following sample, that shows how to organize data to display and edit in TwoNumbersColumn:
[c#]
private void Form1_Load(object sender, System.EventArgs e)
{
  InitFlyGrid(flyGrid);
}

//data class to display
public class Data : IFormattable
{
  public readonly double First;
  public readonly double Second;
  public Data(double first, double second)
  {
    this.First = first;
    this.Second = second;
  }
  #region IFormattable Members

  public string ToString(string format, IFormatProvider formatProvider)
  {
    return First.ToString(format, formatProvider)+\"\\r\\n\" +
      Second.ToString(format, formatProvider);
  }
  #endregion
}

//column that will display and edit two numbers
public class TwoNumbersColumn : NumberColumnBase, ISupportsTranslateValue
{
  public TwoNumbersColumn(string name) : base(name){}          
  public override StringFormatFlags FormatFlags
  {
    get
    {
      StringFormatFlags baseFF =  base.FormatFlags;
      //turn on wordwrap
      baseFF &= ~StringFormatFlags.NoWrap;
      return baseFF;
    }        
  }
  
  #region ISupportsTranslateValue Members
  public object TranslateToValue(object data)
  {
    string strData = data as string;
    if (strData != null)//parse data
    {
      System.Globalization.NumberFormatInfo currentInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
      int sepindex = strData.IndexOf(\"\\r\\n\");
      string first = sepindex != -1 ? strData.Substring(0, sepindex) : strData;
      string second = sepindex != -1 ? strData.Substring(sepindex + 2) : string.Empty;
      double dfirst = first != null && first != string.Empty ? double.Parse(first, currentInfo) : 0;
      double dsecond = second != null && second != string.Empty ? double.Parse(second, currentInfo) : 0;
      return new Data(dfirst, dsecond);
    }
    return null;
  }

  public object TranslateValue(object data)
  {
    return base.GetTextValue(data);
  }

  #endregion
}

private void InitFlyGrid(FlyGrid flyGrid)
{
  flyGrid.BeginInit();
  try
  {
    //increase rows height to display two rows of numbers
    flyGrid.Rows.DefaultRowHeight = 46;
    TwoNumbersColumn twoNumbersColumn = new TwoNumbersColumn(\"Data\");
    //align data
    twoNumbersColumn.TextAlign = ContentAlignment.MiddleRight;
    //fit to width
    twoNumbersColumn.FitMode = ColumnFitMode.Spring;
    flyGrid.Columns.Options |= ColumnsOptions.FitToWidth;
    //add column
    flyGrid.Columns.Items.Add(twoNumbersColumn);
    //add nodes with data (in this sample - randomly generated)
    Random rnd = new Random();
    for(int i=0; i < 10; i++)
    {
      //create data
      Data data = new Data(rnd.NextDouble(), rnd.NextDouble());
      //add node with data
      flyGrid.Rows.Items.Add(new Node(data));
    }      
  }
  finally
  {
    flyGrid.EndInit();
  }
}