DevExpress-GridControl控件-GridView使用

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 14:39   11   0

GridControl在不同版本(目前使用14.1.8)提供了多种不同的视图,它不仅比DataGridView强大,而且在数据加载性能各方面也有了很大的提升。

在此对之前的研究做一份整理记录,以备后用。

------------------------------ 强大的分割线 ------------------------------

表格控件:GridView相当于DataGridView效果,算是GridControl最常使用到的视图。

以下分实现功能进行总结:

1. 添加CheckBox到行头处

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. gridView1.OptionsSelection.CheckBoxSelectorColumnWidth = 40;
  2. gridView1.OptionsSelection.MultiSelect = true;
  3. gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
  4. gridView1.OptionsSelection.ShowCheckBoxSelectorInColumnHeader = DevExpress.Utils.DefaultBoolean.True;

效果图:


2.合并行

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. gridView1.OptionsView.AllowCellMerge = true;
效果图:


3.重绘(GridView)标题CheckBox样式

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. private void YnGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
  2. {
  3. if (e.Column != null && e.Column.Name == "DX$CheckboxSelectorColumn" && this.OptionsSelection.ShowCheckBoxSelectorInColumnHeader != DefaultBoolean.False)
  4. {
  5. bool value = (this.SelectedRowsCount == this.DataRowCount);
  6. RepositoryItemCheckEdit repositoryCheck = null;
  7. if (e.Column.RealColumnEdit == null)
  8. {
  9. repositoryCheck = new RepositoryItemCheckEdit();
  10. }
  11. else
  12. {
  13. repositoryCheck = e.Column.RealColumnEdit as RepositoryItemCheckEdit;
  14. }
  15. e.Info.InnerElements.Clear();
  16. e.Painter.DrawObject(e.Info);
  17. DrawCheckBox(repositoryCheck, e.Graphics, e.Bounds, value);
  18. e.Handled = true;
  19. }
  20. }
  21. protected void DrawCheckBox(RepositoryItemCheckEdit edit, Graphics g, Rectangle r, bool value)
  22. {
  23. DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
  24. DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
  25. DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
  26. info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
  27. painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
  28. info.EditValue = value;
  29. info.Bounds = r;
  30. info.CalcViewInfo(g);
  31. args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
  32. painter.Draw(args);
  33. args.Cache.Dispose();
  34. }

4.添加(GridView)无数据水印

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. protected override void RaiseCustomDrawEmptyForeground(CustomDrawEventArgs e)
  2. {
  3. if (this.RowCount == 0 && !e.Handled)
  4. {
  5. Image img = Properties.Resources.水印_无数据;
  6. RectangleF actualRectF;
  7. int actualHeight = e.Bounds.Height - 26;
  8. if (e.Bounds.Width < img.Width || actualHeight < img.Height)
  9. {
  10. // 当前区域小于图片大小,进行缩放。
  11. float factor1 = e.Bounds.Width * 1f / img.Width;
  12. float factor2 = actualHeight * 1f / img.Height;
  13. float factor = Math.Min(factor1, factor2);
  14. float x = (e.Bounds.Width - img.Width * factor) / 2;
  15. float y = (e.Bounds.Height - img.Height * factor) + 26 / 2;
  16. actualRectF = new RectangleF(x, y, img.Width * factor, img.Height * factor);
  17. }
  18. else
  19. {
  20. actualRectF = new RectangleF((e.Bounds.Width - img.Width) / 2f, (actualHeight - img.Height) / 2f + 26, img.Width, img.Height);
  21. }
  22. e.Graphics.DrawImage(img, actualRectF);
  23. e.Handled = true;
  24. }
  25. base.RaiseCustomDrawEmptyForeground(e);
  26. }

效果图:


5. 重绘(GridView)选中行与悬停行

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. private int hotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
  2. public int HotTrackRow
  3. {
  4. get
  5. {
  6. return hotTrackRow;
  7. }
  8. set
  9. {
  10. if (hotTrackRow != value)
  11. {
  12. int prevHotTrackRow = hotTrackRow;
  13. hotTrackRow = value;
  14. if (this.ActiveEditor != null)
  15. {
  16. this.PostEditor();
  17. }
  18. this.RefreshRow(prevHotTrackRow);
  19. this.RefreshRow(hotTrackRow);
  20. if (hotTrackRow >= 0)
  21. {
  22. GridControl.Cursor = Cursors.Hand;
  23. }
  24. else
  25. {
  26. GridControl.Cursor = Cursors.Default;
  27. }
  28. }
  29. }
  30. }
  31. private void YnGridView_MouseMove(object sender, MouseEventArgs e)
  32. {
  33. GridHitInfo info = this.CalcHitInfo(new Point(e.X, e.Y));
  34. if (info.InRowCell)
  35. {
  36. HotTrackRow = info.RowHandle;
  37. }
  38. else
  39. {
  40. HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
  41. }
  42. }
  43. private void YnGridView_MouseLeave(object sender, EventArgs e)
  44. {
  45. HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
  46. }
  47. protected override DevExpress.Utils.AppearanceObject GetRowCellStyle(int rowHandle, GridColumn column, GridRowCellState state, AppearanceObject appearance)
  48. {
  49. if (rowHandle == HotTrackRow)
  50. {
  51. appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowBackColor;
  52. appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;
  53. }
  54. else if (rowHandle == this.FocusedRowHandle || this.IsRowSelected(rowHandle))
  55. {
  56. appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.SelectedRowBackColor;
  57. appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;
  58. }
  59. return base.GetRowCellStyle(rowHandle, column, state, appearance);
  60. }

效果图:



另外还可参考:GridControl常见用法

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP