DevExpress-GridControl控制元件-GridView使用
阿新 • • 發佈:2019-02-08
GridControl在不同版本(目前使用14.1.8)提供了多種不同的檢視,它不僅比DataGridView強大,而且在資料載入效能各方面也有了很大的提升。
在此對之前的研究做一份整理記錄,以備後用。
------------------------------ 強大的分割線 ------------------------------
表格控制元件:GridView相當於DataGridView效果,算是GridControl最常使用到的檢視。
以下分實現功能進行總結:
1. 新增CheckBox到行頭處
-
gridView1.OptionsSelection.CheckBoxSelectorColumnWidth = 40;
- gridView1.OptionsSelection.MultiSelect = true;
- gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
- gridView1.OptionsSelection.ShowCheckBoxSelectorInColumnHeader = DevExpress.Utils.DefaultBoolean.True;
效果圖:
2.合併行
-
gridView1.OptionsView.AllowCellMerge =
3.重繪(GridView)標題CheckBox樣式
- privatevoid YnGridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
- {
-
if (e.Column != null && e.Column.Name == "DX$CheckboxSelectorColumn" && this.OptionsSelection.ShowCheckBoxSelectorInColumnHeader != DefaultBoolean.False)
- {
- bool value = (this.SelectedRowsCount == this.DataRowCount);
- RepositoryItemCheckEdit repositoryCheck = null;
- if (e.Column.RealColumnEdit == null)
- {
- repositoryCheck = new RepositoryItemCheckEdit();
- }
- else
- {
- repositoryCheck = e.Column.RealColumnEdit as RepositoryItemCheckEdit;
- }
- e.Info.InnerElements.Clear();
- e.Painter.DrawObject(e.Info);
- DrawCheckBox(repositoryCheck, e.Graphics, e.Bounds, value);
- e.Handled = true;
- }
- }
- protectedvoid DrawCheckBox(RepositoryItemCheckEdit edit, Graphics g, Rectangle r, bool value)
- {
- DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
- DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
- DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
- info = edit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
- painter = edit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
- info.EditValue = value;
- info.Bounds = r;
- info.CalcViewInfo(g);
- args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
- painter.Draw(args);
- args.Cache.Dispose();
- }
4.新增(GridView)無資料水印
- protectedoverridevoid RaiseCustomDrawEmptyForeground(CustomDrawEventArgs e)
- {
- if (this.RowCount == 0 && !e.Handled)
- {
- Image img = Properties.Resources.水印_無資料;
- RectangleF actualRectF;
- int actualHeight = e.Bounds.Height - 26;
- if (e.Bounds.Width < img.Width || actualHeight < img.Height)
- {
- // 當前區域小於圖片大小,進行縮放。
- float factor1 = e.Bounds.Width * 1f / img.Width;
- float factor2 = actualHeight * 1f / img.Height;
- float factor = Math.Min(factor1, factor2);
- float x = (e.Bounds.Width - img.Width * factor) / 2;
- float y = (e.Bounds.Height - img.Height * factor) + 26 / 2;
- actualRectF = new RectangleF(x, y, img.Width * factor, img.Height * factor);
- }
- else
- {
- actualRectF = new RectangleF((e.Bounds.Width - img.Width) / 2f, (actualHeight - img.Height) / 2f + 26, img.Width, img.Height);
- }
- e.Graphics.DrawImage(img, actualRectF);
- e.Handled = true;
- }
- base.RaiseCustomDrawEmptyForeground(e);
- }
效果圖:
5. 重繪(GridView)選中行與懸停行
- privateint hotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
- publicint HotTrackRow
- {
- get
- {
- return hotTrackRow;
- }
- set
- {
- if (hotTrackRow != value)
- {
- int prevHotTrackRow = hotTrackRow;
- hotTrackRow = value;
- if (this.ActiveEditor != null)
- {
- this.PostEditor();
- }
- this.RefreshRow(prevHotTrackRow);
- this.RefreshRow(hotTrackRow);
- if (hotTrackRow >= 0)
- {
- GridControl.Cursor = Cursors.Hand;
- }
- else
- {
- GridControl.Cursor = Cursors.Default;
- }
- }
- }
- }
- privatevoid YnGridView_MouseMove(object sender, MouseEventArgs e)
- {
- GridHitInfo info = this.CalcHitInfo(new Point(e.X, e.Y));
- if (info.InRowCell)
- {
- HotTrackRow = info.RowHandle;
- }
- else
- {
- HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
- }
- }
- privatevoid YnGridView_MouseLeave(object sender, EventArgs e)
- {
- HotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;
- }
- protectedoverride DevExpress.Utils.AppearanceObject GetRowCellStyle(int rowHandle, GridColumn column, GridRowCellState state, AppearanceObject appearance)
- {
- if (rowHandle == HotTrackRow)
- {
- appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowBackColor;
- appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;
- }
- elseif (rowHandle == this.FocusedRowHandle || this.IsRowSelected(rowHandle))
- {
- appearance.BackColor = SkinManager.CurrentSkin.GridViewBorderStyle.SelectedRowBackColor;
- appearance.ForeColor = SkinManager.CurrentSkin.GridViewBorderStyle.HoverRowForeColor;
- }
- returnbase.GetRowCellStyle(rowHandle, column, state, appearance);
- }
效果圖: