1. 程式人生 > >DevExpress-GridControl控制元件-GridView使用

DevExpress-GridControl控制元件-GridView使用

GridControl在不同版本(目前使用14.1.8)提供了多種不同的檢視,它不僅比DataGridView強大,而且在資料載入效能各方面也有了很大的提升。

在此對之前的研究做一份整理記錄,以備後用。

------------------------------ 強大的分割線 ------------------------------

表格控制元件:GridView相當於DataGridView效果,算是GridControl最常使用到的檢視。

以下分實現功能進行總結:

1. 新增CheckBox到行頭處

  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.合併行

  1. gridView1.OptionsView.AllowCellMerge = 
    true;  
效果圖:


3.重繪(GridView)標題CheckBox樣式

  1. privatevoid 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. protectedvoid 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)無資料水印

  1. protectedoverridevoid 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)選中行與懸停行

  1. privateint hotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;  
  2. publicint 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. privatevoid 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. privatevoid YnGridView_MouseLeave(object sender, EventArgs e)  
  44. {  
  45.     HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;  
  46. }  
  47. protectedoverride 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.     elseif (rowHandle == this.FocusedRowHandle || this.IsRowSelected(rowHandle))  
  55.     {  
  56.         appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.SelectedRowBackColor;  
  57.         appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;  
  58.     }  
  59.     returnbase.GetRowCellStyle(rowHandle, column, state, appearance);  
  60. }  

效果圖: