Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Hirarchy column

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

Hirarchy column
Link Posted: 29-Sep-2005 02:16
hello,

I define a Hirarchy column. I set
[/code]Column.ShowCheckBoxes = true;

1 )Is there any way that when I check a parent node all his children will be checked?
2) How can i get all the checked nodes?

Thanks,
Link Posted: 29-Sep-2005 02:31
I save the check nodes and close the tree. How can i check the nodes that i saved at the next time i load the tree?
Link Posted: 29-Sep-2005 05:36
[quote="adif"]hello,
I define a Hirarchy column. I set
Column.ShowCheckBoxes = true;
1 )Is there any way that when I check a parent node all his children will be checked?
2) How can i get all the checked nodes?


You can use FlyGrid.NodeCheckedChange event for this purposes:
[c#]
private void InitGrid(FlyGrid flyGrid)
{
  //...some init code
  // connect to NodeCheckedChange event handler
  flyGrid.NodeCheckedChange += new NodeChangeHandler(OnNodeCheckedChange);
}
private bool ignoreNodeChange = false;
private void OnNodeCheckedChange(object sender, NodeBase node)
{
  if (!ignoreNodeChange)
  {
    ignoreNodeChange = true;
    try
    {
      if (node.HasChildren)
      {
        foreach(NodeBase subNode in node.Items)
          CheckNode(subNode, node.Checked);  
      }
    }
    finally
    {
      ignoreNodeChange = false;
    }
  }
}

private void CheckNode(NodeBase node, bool checked)
{
  node.Checked = checked;
  if (node.HasChildren)
  {
    foreach(NodeBase subNode in node.Items)
      CheckNode(subNode, checked);  
  }
}
Link Posted: 29-Sep-2005 06:47
[quote="adif"]I save the check nodes and close the tree. How can i check the nodes that i saved at the next time i load the tree?

[c#]
#region Saving checked nodes
//this method generated node unigue id based on its index and parent node unique Id
private string GetNodeId(NodeBase node)
{
  return node.Parent != null ? GetNodeId(node.Parent) + '.' + node.Index.ToString() : node.Index.ToString();
}

//this method saves checked nodes info into string and returns generated string
private string SaveCheckedNodes(FlyGrid flyGrid)
{
  string listOfCheckedNodes = SaveCheckedNodes(flyGrid.Rows.RootNode);
  return listOfCheckedNodes;
}

//this recursive helper method saves checked nodes info into string and returns generated string  
private string SaveCheckedNodes(NodeBase parentNode)
{
  string listOfCheckedNodes = string.Empty;
  if (parentNode.HasChildren)
  {
    foreach(NodeBase node in flyGrid.Rows.Items)
    {
      if (node.Checked)
      {
        if (listOfCheckedNodes != string.Empty)
          listOfCheckedNodes += ",";
        listOfCheckedNodes += GetNodeId(node);  
        string subNodes = SaveCheckedNodes(node);  
        if (subNodes != string.Empty)
          listOfCheckedNodes += "," + subNodes;
      }
    }    
  }
  return listOfCheckedNodes;
}

#endregion
#region Load Checked nodes
//Extracts node from given nodeId generated by GetNodeId
private NodeBase GetNodeById(FlyGrid flyGrid, string nodeId)
{
  if (nodeId != null && node != string.Empty)
  {
    string[] levels = nodeId.Split('.');
    NodeBase root = flyGrid.Rows.RootNode;
    for(int i = 0; i < levels.Length; i++)
    {
       int nodeIndex = int.Parse(levels[i]);
       try
       {
         NodeBase node = root.Items[nodeIndex];
         root = node;                    
       }
       catch
       {
         MessageBox.Show(string.Format("Node with Id: {0} not found, on level: {1}, index: {2}", nodeId, i, nodeIndex);
       }
    }
    return root;
  }
  return null;
}

//this method extracts nodes from string saved by SaveCheckedNodes method and checks these nodes
//!!! if you use FlyGrid.NodeCheckedChange or FlyGrid.NodeCheckedChanging - disconnect or ignore these events while this method is running
private void LoadCheckedNodes(FlyGrid flyGrid, string savedCheckedNodes)
{
  string[] checkedNodes = savedCheckedNodes.Split(',');
  for(int i = 0; i < checkedNodes.Length; i++)
  {
    try
    {
      NodeBase node = GetNodeById(flyGrid, checkedNodes[i]);
      if (node != null) node.Checked = true;
    }
    catch {}
  }
}
#endregion