DevExpress GridView 鼠标悬停颜色追踪(行或单元格)

DevExpress GridView 鼠标悬停颜色追踪(行或单元格)

2019年07月12日 15:17:02 涛神-Dev 阅读数 41 标签: GridView行追踪单元格追踪 更多

个人分类: DevExpressWinformGridControl

如何将GridView做成类似网页的列表那样,鼠标移动的是行背景颜色跟着变,也就是所为的鼠标悬停追踪,

效果如下:

DevExpress GridView 鼠标悬停颜色追踪(行或单元格)

代码如下:


 
  1. public class ViewStyleHelper

  2. {

  3. bool enable;

  4. public bool Enable

  5. {

  6. get { return enable; }

  7. set

  8. {

  9. enable = value;

  10. UnRegisterEvent();

  11. if (enable)

  12. {

  13. RegisterEvent();

  14. }

  15. else

  16. {

  17.  
  18. View.RefreshData();

  19. }

  20. }

  21. }

  22. bool byRow;

  23. /// <summary>

  24. /// 真为行,假为单元格

  25. /// </summary>

  26. public bool ByRow

  27. {

  28. get { return byRow; }

  29. set

  30. {

  31. byRow = value;

  32. UnRegisterEvent();

  33. if(enable)

  34. RegisterEvent();

  35. }

  36. }

  37. public DevExpress.XtraGrid.Views.Grid.GridView View { get; private set; }

  38. /// <summary>

  39. /// 当前列

  40. /// </summary>

  41. GridColumn currentCol;

  42. /// <summary>

  43. /// 当前行

  44. /// </summary>

  45. int currentRowHandle;

  46. public ViewStyleHelper(DevExpress.XtraGrid.Views.Grid.GridView view,bool byRow=true)

  47. {

  48. View = view;

  49. this.byRow = byRow;

  50. Enable = true;

  51. view.MouseLeave += (s, e) =>

  52. {

  53. currentCol = null;

  54. currentRowHandle = int.MinValue;

  55. view.RefreshData();

  56. };

  57. }

  58. void RegisterEvent()

  59. {

  60. View.MouseMove += onm ouseMove;

  61. if (!byRow)

  62. View.RowCellStyle += OnRowCellStyle;

  63. else

  64. {

  65. View.RowStyle += OnRowStyle;

  66. }

  67. }

  68. void UnRegisterEvent()

  69. {

  70. View.MouseMove -= onm ouseMove;

  71. View.RowCellStyle -= OnRowCellStyle;

  72. View.RowStyle -= OnRowStyle;

  73. }

  74. private void onm ouseMove(object sender, MouseEventArgs e)

  75. {

  76. var view = sender as DevExpress.XtraGrid.Views.Grid.GridView;

  77. var info = view.CalcHitInfo(e.Location);

  78. bool refresh = false;

  79. if (currentCol != info.Column || currentRowHandle != info.RowHandle)

  80. {

  81. refresh = true;

  82. }

  83. if (info.InDataRow)

  84. {

  85. currentCol = info.Column;

  86. currentRowHandle = info.RowHandle;

  87. }

  88. else

  89. {

  90. currentCol = null;

  91. currentRowHandle = int.MinValue;

  92. }

  93. if (refresh)

  94. view.RefreshData();

  95. }

  96. private void OnRowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)

  97. {

  98. if (e.RowHandle == currentRowHandle)

  99. {

  100. e.Appearance.BackColor = Color.FromArgb(108, 178, 235);

  101. e.HighPriority = true;

  102. }

  103.  
  104. }

  105. private void OnRowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)

  106. {

  107. if (e.Column == currentCol && e.RowHandle == currentRowHandle)

  108. {

  109. e.Appearance.BackColor = Color.FromArgb(108, 178, 235);

  110. }

  111.  
  112. }

  113. }

 

调用代码:


 
  1. ViewStyleHelper helper;

  2. private void Form1_Load(object sender, EventArgs e)

  3. {

  4. helper = new ViewStyleHelper(gridView1);

  5. var dt = CreateDt();

  6. gridControl1.DataSource = dt;

  7. }

 

上一篇:javascript – 可能在浏览器中运行PhoneJS?


下一篇:c# – 字符串中字符的限制