G2DTreeDataGrid.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using AdvancedDataGridView;
  10. using System.Reflection;
  11. using System.Collections;
  12. using CommonLang.Property;
  13. namespace CommonFroms.Utils.TreeGrid
  14. {
  15. public partial class G2DTreeDataGrid : UserControl
  16. {
  17. private G2DTreeGridDocument selected_doc ;
  18. public readonly Image img_icon_men;
  19. public G2DTreeDataGrid()
  20. {
  21. InitializeComponent();
  22. this.img_icon_men = imageList1.Images["icon_men.png"];
  23. }
  24. //-------------------------------------------------------------------------------------------
  25. public object SelectedObject
  26. {
  27. get
  28. {
  29. if (selected_doc != null)
  30. {
  31. return selected_doc.Value;
  32. }
  33. return null;
  34. }
  35. set
  36. {
  37. SetSelectedObject(value);
  38. }
  39. }
  40. public G2DTreeGridValueNode CurrentValueNode
  41. {
  42. get
  43. {
  44. return treeGridView1.CurrentNode as G2DTreeGridValueNode;
  45. }
  46. }
  47. private void SetSelectedObject(object obj)
  48. {
  49. if (obj != null)
  50. {
  51. selected_doc = new G2DTreeGridDocument(this);
  52. treeGridView1.Nodes.Clear();
  53. treeGridView1.Nodes.Add(selected_doc);
  54. selected_doc.Value = (obj);
  55. treeGridView1.ExpandNode(selected_doc);
  56. treeGridView1.ExpandNode(selected_doc.DocumentElement);
  57. }
  58. else
  59. {
  60. selected_doc = null;
  61. treeGridView1.Nodes.Clear();
  62. }
  63. }
  64. private void SelectNode(int rowIndex)
  65. {
  66. try
  67. {
  68. TreeGridNode node = treeGridView1.GetNodeForRow(rowIndex);
  69. if (node != treeGridView1.CurrentNode)
  70. {
  71. treeGridView1.ClearSelection();
  72. node.Selected = true;
  73. treeGridView1.CurrentCell = node.Cells[1];
  74. treeGridView1.Invalidate();
  75. }
  76. }
  77. catch (Exception err) { }
  78. }
  79. //-------------------------------------------------------------------------------------------
  80. #region _Windows事件处理_
  81. // 选择 //
  82. private void treeGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
  83. {
  84. SelectNode(e.RowIndex);
  85. }
  86. private void treeGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  87. {
  88. SelectNode(e.RowIndex);
  89. }
  90. private void menuNode_Opening(object sender, CancelEventArgs e)
  91. {
  92. if (CurrentValueNode != null)
  93. {
  94. CurrentValueNode.OnMenuOpening(e);
  95. }
  96. }
  97. private void menuNode_New_Click(object sender, EventArgs e)
  98. {
  99. if (CurrentValueNode != null)
  100. {
  101. CurrentValueNode.OnNewContentClick();
  102. }
  103. }
  104. private void menuNode_Delete_Click(object sender, EventArgs e)
  105. {
  106. if (CurrentValueNode != null)
  107. {
  108. CurrentValueNode.OnDelContentClick();
  109. }
  110. }
  111. private void menuNode_Up_Click(object sender, EventArgs e)
  112. {
  113. if (CurrentValueNode != null)
  114. {
  115. CurrentValueNode.OnMoveListItemIndexClick(-1);
  116. }
  117. }
  118. private void menuNode_Down_Click(object sender, EventArgs e)
  119. {
  120. if (CurrentValueNode != null)
  121. {
  122. CurrentValueNode.OnMoveListItemIndexClick(1);
  123. }
  124. }
  125. #endregion
  126. //-------------------------------------------------------------------------------------------
  127. }
  128. }