FormGridKeyDown.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using Infragistics.Win;
  11. using Infragistics.Win.UltraWinGrid;
  12. namespace Core.StlMes
  13. {
  14. public partial class FormGridKeyDown : Form
  15. {
  16. public FormGridKeyDown()
  17. {
  18. InitializeComponent();
  19. }
  20. private void FormGridKeyDown_Load(object sender, EventArgs e)
  21. {
  22. //setColumnNoEdit();
  23. initData();
  24. }
  25. private void setColumnNoEdit()
  26. {
  27. foreach (UltraGridColumn c in ultraGrid1.DisplayLayout.Bands[0].Columns)
  28. {
  29. c.CellActivation = Activation.NoEdit;
  30. }
  31. }
  32. private void initData()
  33. {
  34. DataRow dr = null;
  35. for (int i = 0; i < 5; i++)
  36. {
  37. dr = dataTable1.NewRow();
  38. for (int j = 0; j < 5; j++)
  39. {
  40. dr[j] = (i + 1).ToString();
  41. }
  42. dataTable1.Rows.Add(dr);
  43. }
  44. for (int i = 0; i < 5; i++)
  45. {
  46. dr = dataTable2.NewRow();
  47. for (int j = 0; j < 5; j++)
  48. {
  49. dr[j] = (i + 1).ToString();
  50. }
  51. dataTable2.Rows.Add(dr);
  52. dr = dataTable2.NewRow();
  53. for (int j = 0; j < 5; j++)
  54. {
  55. dr[j] = (i + 1).ToString();
  56. }
  57. dataTable2.Rows.Add(dr);
  58. dr = dataTable2.NewRow();
  59. for (int j = 0; j < 5; j++)
  60. {
  61. dr[j] = (i + 1).ToString();
  62. }
  63. dataTable2.Rows.Add(dr);
  64. }
  65. }
  66. private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
  67. {
  68. int deviation = 3;
  69. try
  70. {
  71. UltraGrid grid = (UltraGrid)sender;
  72. if (grid.ActiveCell != null)
  73. {
  74. Rectangle rect = grid.ActiveCell.GetUIElement().Rect;
  75. switch (e.KeyCode)
  76. {
  77. case Keys.Up:
  78. rect.X = rect.X + deviation;
  79. rect.Y = rect.Y - deviation;
  80. e.Handled = true;
  81. break;
  82. case Keys.Down:
  83. case Keys.Enter:
  84. rect.X = rect.X + deviation;
  85. rect.Y = rect.Y + rect.Height + deviation;
  86. e.Handled = true;
  87. break;
  88. case Keys.Left:
  89. Left:
  90. rect.X = rect.X - deviation;
  91. rect.Y = rect.Y + deviation;
  92. e.Handled = true;
  93. break;
  94. case Keys.Right:
  95. Right:
  96. rect.X = rect.X + rect.Width + deviation;
  97. rect.Y = rect.Y + deviation;
  98. e.Handled = true;
  99. break;
  100. case Keys.Tab:
  101. if (e.Shift)
  102. {
  103. goto Left;
  104. }
  105. else
  106. {
  107. goto Right;
  108. }
  109. break;
  110. }
  111. UIElement myUIElement = this.ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(new Point(rect.X,rect.Y));
  112. UltraGridCell cell = (UltraGridCell)myUIElement.GetContext(typeof(UltraGridCell));
  113. if (cell != null && e.Handled)
  114. {
  115. ultraGrid1.ActiveCell = cell;
  116. }
  117. grid.PerformAction(UltraGridAction.EnterEditMode);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. //CoreWriteLogFileBase.WriteLog(ex.Message, "", "样式错误", LogInfoLevel.Error);
  123. }
  124. }
  125. private void ultraGrid1_KeyPress(object sender, KeyPressEventArgs e)
  126. {
  127. e.Handled = false;
  128. }
  129. private void ultraGrid1_ClickCell(object sender, ClickCellEventArgs e)
  130. {
  131. Rectangle rect =e.Cell.GetUIElement().Rect;
  132. //MessageBox.Show(rect.X.ToString() + " , " + rect.Y.ToString() + " , " + rect.Width.ToString() + " , " + rect.Height.ToString());
  133. }
  134. }
  135. }