Home - Forums-.NET - FlyGrid.Net (Windows Forms) - C#: Additional samples

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

C#: Additional samples
Link Posted: 03-Oct-2006 07:37
This code helps to scan(enumerate) nodes in the nested grids or mixed (trees and/or nested grids):
[c#]
private void ScanNodes(FlyGrid flyGrid)
{
  //initial call - scan a root port
  ScanNodesFromPort(flyGrid.ActiveRootPort, 0);
}

private void ScanNodesFromPort(FlyGridViewPort port, int indent)
{
  foreach(NodeBase node in port.Rows.Items)
  {
    EnumerateCells(node, port.Columns, indent);
    if (node.HasChildren)
    {
      NestedGridNode nested = node as NestedGridNode;
      if (nested != null)
      {
        nested.Expanded = true;//it it necessary to get nested port
        ScanNodesFromPort(nested.NestedInternal, indent+1);
      }
      else
        ScanNodesFromItems(node.Items, port.Columns, indent+1);
    }
  }
}
private void ScanNodesFromItems(NodeCollection nodes, Columns columns, int indent)
{
  foreach(NodeBase node in nodes)
  {
    EnumerateCells(node, columns, indent);
    if (node.HasChildren)
    {
      NestedGridNode nested = node as NestedGridNode;
      if (nested != null)
      {
        nested.Expanded = true;//it it necessary to get nested port
        ScanNodesFromPort(nested.NestedInternal, indent+1);
      }
      else
        ScanNodesFromItems(node.Items, columns, indent+1);
    }
  }
}
private void EnumerateCells(NodeBase node, Columns cols, int indent)
{
  foreach(Column col in cols.VisibleColumns)
  {
    System.Diagnostics.Debug.WriteLine(GetIndentString(indent) + node[col].ToString());        
  }
}

private string GetIndentString(int indent)
{
  return new string(' ', 2*indent);
}
Link Posted: 02-Feb-2007 10:05
This code help you to solve problem with cloning nodes (missing NodeBase.Clone() method analogue):
[c#]
public static NodeBase DeserializeNode(SerializationInfo info)
{
  System.Runtime.Remoting.ObjectHandle obj = System.Activator.CreateInstance(info.AssemblyName, info.FullTypeName);
  if (obj != null)
  {
    NodeBase newNode =  obj.Unwrap() as NodeBase;        
    if (newNode != null)
    {
      PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(newNode);
      foreach (SerializationEntry se in info)
      {
        PropertyDescriptor pd = pdc[se.Name];
        if (pd != null && !pd.IsReadOnly)
          pd.SetValue(newNode, se.Value);
      }
      return newNode;
    }
  }
  throw new SerializationException(string.Format(\"Type [{0}] not found, node can't be serialized\"));
}

public static SerializationInfo SerializeNode(NodeBase node)
{
  SerializationInfo info = new SerializationInfo(node.GetType(), new FormatterConverter());
  PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(node);
  foreach(PropertyDescriptor pd in pdc)
  {        
    if (pd.ShouldSerializeValue(node) && pd.SerializationVisibility != DesignerSerializationVisibility.Hidden)
    {
      object pdvalue = pd.GetValue(node);
      info.AddValue(pd.Name, pdvalue, pd.PropertyType);
    }
  }
  return info;
}

public static NodeBase CloneNode(NodeBase node)
{
  SerializationInfo info = SerializeNode(node);
  return DeserializeNode(info);
}
Link Posted: 02-Feb-2007 10:23
Tried CloneNode, but still any changes made to source node are also done in the target node and vice-versa

TargetNode.Items.Add(NodeBase.CloneNode(SourceNode));
Link Posted: 02-Feb-2007 10:33
You can improve this method by cloning property values:
[c#]
public static SerializationInfo SerializeNode(NodeBase node)
{
  SerializationInfo info = new SerializationInfo(node.GetType(), new FormatterConverter());
  PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(node);
  foreach(PropertyDescriptor pd in pdc)
  {      
    if (pd.ShouldSerializeValue(node) && pd.SerializationVisibility != DesignerSerializationVisibility.Hidden)
    {
      object pdvalue = pd.GetValue(node);
      //if object is implementing ICloneable - clone this object
      ICloneable cloneable = pdvalue as ICloneable;
      if (cloneable != null)
        pdvalue = cloneable.Clone();
      info.AddValue(pd.Name, pdvalue, pd.PropertyType);
    }
  }
  return info;
}
Link Posted: 07-Feb-2007 05:12
This worked better for me, as the serialization was too time consuming:

Node NewNode = new Node ();

object[] Value = (object [])this.Value;
NewNode.Value = Value.Clone();

NewNode.ImageIndex = this.ImageIndex;
NewNode.SelectedImageIndex = this.SelectedImageIndex;
Link Posted: 07-Feb-2007 05:42
Yes, but this code good for usual node, not for the custom node,
anyway, here is more effective code (based on your code):
[c#]
Node NewNode = new Node (this.Value.Clone());
NewNode.ImageIndex = this.ImageIndex;
NewNode.SelectedImageIndex = this.SelectedImageIndex;
Link Posted: 25-Feb-2007 13:16
This c# code sample shows how to implement Outlook-like cells fetching - when data is scrolled cells don't display cell values until scrolling is finished.
Link Posted: 02-May-2007 07:25
This sample shows how to customize showing/hiding checkboxes in nodes.
Link Posted: 27-Apr-2009 04:01
This sample shows how to make HierarchyColumn to support Simple or Spin editor styles for different levels of nodes.
Link Posted: 06-Aug-2009 12:16
This c# sample shows how to make column that shows 3 progress bars and how to create and use array of ProgressBar/TrackBar controls as dropdown to edit 3 progress bar data. This sample does also show how to use complex data as cell values.