DevExpress-GridControl控件-GridView使用

来源:互联网 发布:linux 修改home目录 编辑:程序博客网 时间:2024/05/03 13:45

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.   
  16.         e.Info.InnerElements.Clear();  
  17.         e.Painter.DrawObject(e.Info);  
  18.         DrawCheckBox(repositoryCheck, e.Graphics, e.Bounds, value);  
  19.         e.Handled = true;  
  20.     }  
  21. }  
  22. protected void DrawCheckBox(RepositoryItemCheckEdit edit, Graphics g, Rectangle r, bool value)  
  23. {  
  24.     DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;  
  25.     DevExpress.XtraEditors.Drawing.CheckEditPainter painter;  
  26.     DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;  
  27.     info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;  
  28.     painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;  
  29.     info.EditValue = value;  
  30.     info.Bounds = r;  
  31.     info.CalcViewInfo(g);  
  32.     args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);  
  33.     painter.Draw(args);  
  34.     args.Cache.Dispose();  
  35. }  

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.   
  19.             this.RefreshRow(prevHotTrackRow);  
  20.             this.RefreshRow(hotTrackRow);  
  21.   
  22.             if (hotTrackRow >= 0)  
  23.             {  
  24.                 GridControl.Cursor = Cursors.Hand;  
  25.             }  
  26.             else  
  27.             {  
  28.                 GridControl.Cursor = Cursors.Default;  
  29.             }  
  30.         }  
  31.     }  
  32. }  
  33. private void YnGridView_MouseMove(object sender, MouseEventArgs e)  
  34. {  
  35.     GridHitInfo info = this.CalcHitInfo(new Point(e.X, e.Y));  
  36.   
  37.     if (info.InRowCell)  
  38.     {  
  39.         HotTrackRow = info.RowHandle;  
  40.     }  
  41.     else  
  42.     {  
  43.         HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;  
  44.     }  
  45. }  
  46. private void YnGridView_MouseLeave(object sender, EventArgs e)  
  47. {  
  48.     HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;  
  49. }  
  50. protected override DevExpress.Utils.AppearanceObject GetRowCellStyle(int rowHandle, GridColumn column, GridRowCellState state, AppearanceObject appearance)  
  51. {  
  52.     if (rowHandle == HotTrackRow)  
  53.     {  
  54.         appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowBackColor;  
  55.         appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;  
  56.     }  
  57.     else if (rowHandle == this.FocusedRowHandle || this.IsRowSelected(rowHandle))  
  58.     {  
  59.         appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.SelectedRowBackColor;  
  60.         appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;  
  61.     }  
  62.   
  63.     return base.GetRowCellStyle(rowHandle, column, state, appearance);  
  64. }  

效果图:



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

0 0