`
xitongyunwei
  • 浏览: 923881 次
文章分类
社区版块
存档分类
最新评论

DevExpress控件GridControl使用总结一

 
阅读更多

本文参考了部分网上资源

设置选中行的背景色、而不改变前景色。

EnableAppearanceFocusedCell = False, EnableAppearanceFocusedRow = False
private void gdvMarket_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gdvMarket.FocusedRowHandle)
            {
                
               e.Appearance.BackColor=Color.CadetBlue;
              ;
            }
        }


单元格颜色的设置。

//最低价颜色控制

            DevExpress.XtraGrid.StyleFormatCondition lowPrice = new DevExpress.XtraGrid.StyleFormatCondition();
            lowPrice.Column = LowPrice;
            lowPrice.Appearance.ForeColor = Color.Red;
            lowPrice.Appearance.Options.UseForeColor = true;
            lowPrice.Condition = DevExpress.XtraGrid.FormatConditionEnum.Expression;
            lowPrice.Expression = "[LowPrice] > [PrevPrice]";
                this.gdvMarket.FormatConditions.Add(lowPrice);

            //涨跌颜色控制
            DevExpress.XtraGrid.StyleFormatCondition range = new DevExpress.XtraGrid.StyleFormatCondition();
            range.Column = Range;
            range.Appearance.ForeColor = Color.Red;
            range.Appearance.Options.UseForeColor = true;
            range.Condition = DevExpress.XtraGrid.FormatConditionEnum.Greater;
            range.Value1 = 0;
                this.gdvMarket.FormatConditions.Add(range);


单元格字符格式化方式

         this.gdvMarket.Columns["RangePercent"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
               this.gdvMarket.Columns["RangePercent"].DisplayFormat.FormatString = "{0}%";


设置列背景色

            this.gdvMarket.Columns["Amount"].AppearanceCell.BackColor = Color.AliceBlue;
                this.gdvMarket.Columns["Amount"].AppearanceCell.Options.UseBackColor = true;


GridView右键菜单

一、添加右键菜单

1.VS工具箱中的菜单和工具栏找到ContextMenuStrip控件,双击添加。

2.点击ContextMenuStrip右上方的小三角形,打开编辑项,可以添加菜单项。至于菜单点击事件,这里就不多说了。

3.选择gridControl(注意这里不是gridView的属性),在属性中可以找到ContextMenuStrip属性,设置成刚添加的ContextMenuStrip

这样的话,运行起来右击表格就可以看到右键菜单了。

二、是否可用设置

在不同情况下,例如选中行的个数以及内容的不同,右键菜单的菜单项是否可用需要作出判断,

这里需要用到gridViewPopupMenuShowing这个事件。也就是在菜单出现之前用户点击右键之后,来判断一下选择了几行,从而决定菜单项是否可用。

private void gridView_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            //获取选择的行数
            int select = gridView.SelectedRowsCount;
            itemOpen.Enabled = false;
            itemDelete.Enabled = false;
            if(select == 1)
            {
                itemOpen.Enabled = true;
                itemDelete.Enabled = true;
            }
            else if(select > 1)
            {
                itemDelete.Enabled =true;
            }
       }


实现拖拽多选

GridView可以通过Shift键或Ctrl键以及Ctrl+A快捷键实现多选,但是默认不支持拖拽多选,好像也没有设置的方法。这样虽然没什么问题,但是肯定会给用户带来不便。
首先要设置OptionsSelection中的MultiSelecttrue,也就是允许多选,否则下面的一切都是浮云。

本文通过以下代码实现拖拉多选的功能,主要是编写MouseDownMouseMoveMouseUp三个函数。

这里需要注意一下GridHitInfo,这个类可以根据xy坐标获取该点在GridView中的相关信息,例如在哪行哪列哪个单元格内,或者是否在单元格里。

 //用于记录,鼠标是否已按下
        bool isMouseDown = false;

        //用于鼠标拖动多选,标记是否记录开始行
        bool isSetStartRow = false;

        //用于鼠标拖动多选,记录开始行
        private int StartRowHandle = -1;

        //用于鼠标拖动多选,记录现在行
        private int CurrentRowHandle = -1;

        //用于实现鼠标拖动选择多行功能中的一个方法,对单元格区域进行选中
        private void SelectRows(int startRow, int endRow)
        {
            if (startRow > -1 && endRow > -1)
            {
                gridView.BeginSelection();
                gridView.ClearSelection();
                gridView.SelectRange(startRow, endRow);
                gridView.EndSelection();
            }
        }

        //实现鼠标拖动选择多行 ,鼠标按下事件
        private void gridView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
            }
        }

        //实现鼠标拖动选择多行 ,鼠标移动时
        private void gridView_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                GridHitInfo info = gridView.CalcHitInfo(e.X, e.Y);
                //如果鼠标落在单元格里
                if (info.InRow)
                {
                    if (!isSetStartRow)
                    {
                        StartRowHandle = info.RowHandle;
                        isSetStartRow = true;
                    }
                    else
                    {
                        //获得当前的单元格
                        int newRowHandle = info.RowHandle;
                        if (CurrentRowHandle != newRowHandle)
                        {
                            CurrentRowHandle = newRowHandle;
                            //选定 区域 单元格
                            SelectRows( StartRowHandle, CurrentRowHandle);
                        }
                    }
                }
            }
        }

        //实现鼠标拖动选择多行 ,鼠标放开时
        private void gridView_MouseUp(object sender, MouseEventArgs e)
        {
            StartRowHandle = -1;
            CurrentRowHandle = -1;
            isMouseDown = false;
            isSetStartRow = false;
        }


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics