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;
        }


 

發佈了112 篇原創文章 · 獲贊 19 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章