10346 : Getting the clicked Node

Question

A gantt in my application has a treeivew of GridNodes, and I would like to enable the users to right click on the root nodes (which would bring up a context menu) and select a delete menu item to delete that node and all of the child nodes. I only want the user to see a menu if they click on a root node.

So far I have been unable to get the index of the grid node that I clicked on to see if its a root node. I started off with GanttRowFromY(e.Y).GridNode, but the node that is returned always has an index of 0 and the .ParentNode is never null (but the .ParentNode.ParentNode) is always null – irrespective on which GridNode I right-click on.

Could you send a code sample where you get the GridNode which the user clicks on? Also, is there a place where I can store an object that will reference back to the data record which is responsible for the creation of this node? Similar to what you can do with the TimeItem.UserReference property.

Answer

The reason for GanttRowFromY(e.Y).GridNode failing is probably because you send in a coordinate from the Grid Context (from OnGridMouseDown maybe) and the grid and timeitem area are not aligned due to column heads of the grid.

This is the way to do it:


    private void gantt1_OnGridMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
      Cell cell=gantt1.Grid.GridStructure.CellFromXY(e.X,e.Y);
      if ((cell!=null) && (e.Clicks==2)) 
      {
        MessageBox.Show(cell.Node.SubLevel.ToString()+" - "+ cell.Node.Index.ToString());
      }
    }
If you only want to show the popup for root nodes you should check that Node.SubLevel==0.
You also had a question on where to store a reference in the GridNode; The answer is GridNode.UserReference :-)

 

 

 

Leave a Reply