DevExpress控件使用方法:第一篇 gridControl详解

 GridControl

(1)层次设计器

 

有五种视图模式,banded gridview多行表头,数据还是一行一组,最靠近数据的表头与数据一一对应;advanced banded gridview多行表头,数据可以多行一组;cardview 一个卡片是一组数据,其中左侧是标题,右侧是数据;layout view是card view的集合,布局可以自定义

 

 

(2)视图

视图的层次结构

 

视图属性:

可通过GridControl.Views访问所有视图。

可通过ColumnView.Columns访问当前视图的所有列

(3)设计器(层次设计器中点击Run Designer可调出)

设计器有四大功能:①主要功能的设计;②选择外观;③可以选择多个视图,内嵌编辑器的设置;④打印功能的设置

①       View:

(a)Option:

OptionsBehavior可以设置行为(是否允许新增行/是否允许删除行/是否允许编辑/是否允许展开所有分组)

OptionsCustomization(是否允许排序/分组/过滤/列的移动/列的大小的调整)

OptionsDetail(设置从表的属性)、OptionsFilter(过滤属性的设置)OptionsView(显示或隐藏某些东西,比如标题行)

(b)AppearancePrint :  (行高…)

Columns://列的相应的属性

FeatureBrowser://设置事件,Grid的绑定、列的绑定(有方法提示),什么都可以设置

Layout:设置布局

Group Summary Items添加分组统计

②       Appearance:

Appearances可设置行的外观

Format Conditions条件样式

Style Schemes可设置主题(比较方便)

③       Repository

View Repository设计视图,与①差不多

In-place Editor Repository内嵌编辑器

④       Printing

(4)gridControl:给该列添加组件(如按钮)  三种方法

法一:打开设计器(Run Designer),选择左侧Repository,点击In-place Editor Repository(内嵌编辑器),点击Add右侧的下拉菜单,选择你想添加的控件,比如ComboBoxEdit,再在右侧编辑控件的属性,ComboBox的话就可以设置Data下的Items集合,然后点左侧Main里的Columns,点你想在上头添加控件的列,找右侧Data下的ColumnEdit,点最右边的下拉菜单,点Existing左侧的加号,就能找到你刚才的控件,添加。会默认给该列的每一个格都加上这个控件。

 

法二:点击你想添加控件的列标题,再点属性栏中的ColumnEdit,下拉菜单点new,选择想添加的控件,然后Column Edit左侧会出现加号,展开加号,下方会出现许多子属性,这些都是设置内嵌的控件的属性的。

法三:直接写代码。

②       拖动一个gridControl控件,改变视图为BandedGridview,属性栏中点OptionView,选择NewItemRowPosition,选Bottom

②添加引用:usingDevExpress.XtraGrid.Views.BandedGrid;

form的构造函数中添加代码:

BandedGridColumn newColumn = bandedGridView1.Columns.Add() as BandedGridColumn;

           newColumn.Caption = "Country";

           bandedGridView1.Bands[0].Columns.Add(newColumn);

            newColumn.Visible = true;

②(假设添加的控件是计算器)添加引用:usingDevExpress.XtraEditors.Repository;

form的构造函数中继续添加代码,效果图

RepositoryItemCalcEdit calcEdit = new RepositoryItemCalcEdit();

           gridControl.RepositoryItems.Add(calcEdit);

            newColumn.ColumnEdit = calcEdit;

(5)给gridview添加筛选器

              点击gridview1,再点属性栏中的OptionsView,下头有个ShowFilterPanelMode,默认的是不显示,改成总是显示,这样gridview下方就会出现一个筛选器,可以根据条件对数据进行筛选,再显示。

(6)gridview导出为PDF

可以在窗体中添加个按钮,把gridview导出为PDF并在Adobe Reader中显示,前提是电脑上安装的Adobe Reader.

按钮中添加如下代码:

DevExpress.XtraGrid.Views.Grid.GridView View = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;

            if(View != null)

                View.ExportToPdf("ShowData.pdf");//pdf的文件名必须是英文

 

            ProcesspdfExport = new Process();

            pdfExport.StartInfo.FileName = "AcroRd32.exe";

            pdfExport.StartInfo.Arguments = "ShowData.pdf";

            pdfExport.Start();

添加引用using System.Diagnostics;

实际效果图:(列标题无法显示,可能是汉字字体的问题,图中默认为宋体)

 

(7)鼠标滑过gridview时,鼠标所指行显示橙色

①添加两个引用:usingDevExpress.XtraGrid.Views.Grid;

usingDevExpress.XtraGrid.Views.Grid.ViewInfo;

②声明一个私有成员变量:private inthotTrackRow = DevExpress.XtraGrid.GridControl.InvalidRowHandle;

③写一个方法:  private int HotTrackRow {

            get{

                returnhotTrackRow;

            }

            set{

                if(hotTrackRow != value)

                {

                    intprevHotTrackRow = hotTrackRow;

                    hotTrackRow = value;

 

                   gridView1.RefreshRow(prevHotTrackRow);

                    gridView1.RefreshRow(hotTrackRow);

 

                    if(hotTrackRow >= 0)

                        gridControl1.Cursor = Cursors.Hand;

                    else

                        gridControl1.Cursor = Cursors.Default;

                }

            }

       }

④   在gridview的属性栏中找到事件,添加一个MouseMove事件:

 private voidgridView1_MouseMove(object sender, MouseEventArgs e)

       {

            GridViewview = sender as GridView;

            GridHitInfoinfo = view.CalcHitInfo(new Point(e.X, e.Y));

 

            if(info.InRowCell)

                HotTrackRow = info.RowHandle;

            else

                HotTrackRow =DevExpress.XtraGrid.GridControl.InvalidRowHandle;

       }

⑤   在gridview的属性栏中找到事件,给gridview添加一个RowCellStyle事件:

private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgse)

       {

            if(e.RowHandle == HotTrackRow)

                e.Appearance.BackColor = Color.PaleGoldenrod;

       }

效果图:(鼠标滑过时,鼠标所指行显示橙色)

(8)gridview中的当前所选框四周加粗,效果图:

 

①   添加两个引用:using System.Reflection;

usingDevExpress.Utils.Paint;

②再写一个类:

public class MyXPaint : XPaint

   {

       public overridevoid DrawFocusRectangle(Graphics g, Rectangle r, Color foreColor, ColorbackColor)

       {

            if(!CanDraw(r)) return;

            Brushhb = Brushes.Black;

            g.FillRectangle(hb, new Rectangle(r.X,r.Y, 2, r.Height - 2));//left

            g.FillRectangle(hb, new Rectangle(r.X,r.Y, r.Width - 2, 2));//top

            g.FillRectangle(hb, new Rectangle(r.Right-2,r.Y,2, r.Height-2));//right

            g.FillRectangle(hb, new Rectangle(r.X,r.Bottom-2,r.Width, 2));//bottom

       }

}

③在当前form的load事件中添加两行代码:

FieldInfo fi = typeof(XPaint).GetField("graphics",BindingFlags.Static | BindingFlags.NonPublic);

            fi.SetValue(null, new MyXPaint());

(9)gridControl内嵌导航栏(可以翻页、添加或删除行)

点击gridControl1,点属性栏中最下方的UseEmbeddedNavigator,选true,gridview下方就会出现内嵌导航栏

删除行时可以添加提示按钮,方法:

      添加引用using DevExpress.XtraEditors;

      点击gridControl1,点属性栏中的事件,双击EmbeddedNavigator中的ButtonClick事件,添加代码:

if(e.Button.ButtonType == NavigatorButtonType.Remove)

            {

if (MessageBox.Show("你想删除当前行么?", "确认删除", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question) != DialogResult.Yes)

                    e.Handled = true;

            }

 

翻页:如果只要翻页按钮,可以把其余按钮设为不可见

gridControl1属性 EmbededNavigator-Buttons—PrevPage和NextPage的visible设为true,其余的visible全设为false

(10)右键菜单(只能对当前单元格进行复制、粘贴、全选、剪切、操作)

选择gridView1,在属性栏的事件中双击ShownEditor事件,添加代码,效果图:↑

      System.Windows.Forms.ContextMenu cm;

       private voidgridView1_ShownEditor(object sender, EventArgs e)

       {

            if(cm == null)

            {

                InitializeCustomontextMenu();

            }

            this.bandedGridView1.ActiveEditor.ContextMenu= cm;

       }

       private voidInitializeCustomontextMenu()

       {

            cm = newSystem.Windows.Forms.ContextMenu();

            cm.MenuItems.Add(new MenuItem("剪切", new EventHandler(OnCut)));

            cm.MenuItems.Add(new MenuItem("复制", new EventHandler(OnCopy)));

            cm.MenuItems.Add(new MenuItem("粘贴", new EventHandler(OnPaste)));

            cm.MenuItems.Add("-");

            cm.MenuItems.Add(new MenuItem("全选", new EventHandler(OnSelectAll)));

       }

       private voidOnCut(object sender, EventArgse)

       {

            TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;

            if(textEdit != null)

            {

                textEdit.Cut();

            }

       }

       private voidOnCopy(object sender, EventArgse)

       {

            TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;

            if(textEdit != null) {

                textEdit.Cut();

            }

       }

       private voidOnPaste(object sender, EventArgse)

       {

            TextEdittextEdit = this. gridView1.ActiveEditor as TextEdit;

            if(textEdit != null)

            {

                textEdit.Paste();

            }

       }

       private voidOnSelectAll(object sender, EventArgs e)

       {

            TextEdittextEdit = this.gridView1.ActiveEditor as TextEdit;

            if(textEdit != null)

            {

                textEdit.SelectAll();

            }

       }

(12)对某列进行求和或取最大值最小值,显示在gridview底部。

gridview1绑定数据后,点击Run Designer,点Column,右侧点Data下的

Symmary Item,Summary Type的选项根据需求来选。然后关闭当前窗口,gridView1的属性设置页脚可见(属性栏中点OptionView,ShowFooter改为True)。该列的页脚的单元格颜色可以设置。选择gridView1事件中的CustomDrawFooterCell,添加如下代码,再添加引用UsingSystem.Drawing.Drawing2D;

 

(13)gridView不可编辑

OptionBehavior的Editable改为false

(14)单元格添加颜色,效果图:

 

在gridView1的RowCellStyle事件中添加如下代码:

if (e.RowHandle != bandedGridView1.FocusedRowHandle &&((e.RowHandle % 2 == 0 && e.Column.VisibleIndex % 2 == 1) ||(e.Column.VisibleIndex % 2 == 0 && e.RowHandle % 2 == 1)))

                e.Appearance.BackColor = Color.AliceBlue;

(15)gridView按某列标题进行分组(各个组可以展开收起,分组条件为用户的拖拽列,把列标题拖拽到最上方即可),效果图:

 

添加引用:using DevExpress.XtraGrid.Views.Base;

在gridView1的FocusedRowChanged事件中添加代码:

if (gridView1.IsGroupRow(e.FocusedRowHandle))

            {

                boolexpanded = gridView1.GetRowExpanded(e.FocusedRowHandle);

                gridView1.SetRowExpanded(e.FocusedRowHandle,!expanded);

            }

(15)根据单元格的值改变其背景色(右图是将Date列中,值小于2015的单元格染色)

双击进入gridView1的CustomDrawCell事件:

private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgse)

       {

            if(e.Column.FieldName == "Date")

            {

                intx = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle,"Date"));

                if(x < 2015)

                    e.Appearance.BackColor=Color.MistyRose;

            }

            }

(16)列对齐方式  gridView1.Columns["Date"].AppearanceCell.TextOptions.HAlignment= DevExpress.Utils.HorzAlignment.Near;

//Near左对齐    Center居中对齐    Far右对齐    Default数据默认的对齐方式

(17)gridControl绑定数据源DataTable

 

 

 

出处:https://blog.csdn.net/a462575515/article/details/51378067

posted on 2019-01-31 11:01  jack_Meng  阅读(16921)  评论(0编辑  收藏  举报

导航