DevExpress GridControl功能總結


寫在前面,Dev控件已經很久了,功能也很強大,截止到現在我編寫文檔出來的Dev的版本已經到了14.1了,看了Demo真的很強大,效果也很好,結合自己這一個月開發,分享一下自己研究過后的經驗,不讓大家走更多的彎路

  • DevExpress安裝順序
  • GridControl基本樣式設置
  • GridControl常用事件和描述
  • GridControl右鍵菜單
  • GridControl全選和反選
  • GridControl添加小計功能
  • GridControl固定表頭

    1.安裝從未如此簡單

    Dev的安裝文件已經放大百度雲中方便大家下載:DevExpress 12.2.7,下載后按照圖片數字順序依次安裝,這就是Dev 12.2.7,你值得擁有。

    if (bandedGridView1.GetFocusedDataRow() == null) return; //運單編號
                var columnValue= bandedGridView1.GetFocusedRowCellValue("綁定列字段名稱").ToString();

     

     

     

    注意事項:使用VS2012和2010搭配DevExpress 12.2.7這個版本一起開發是最好的,工具箱暫時無法導入VS2013

    2.基本樣式設置,你會嗎?

    面對第一次使用GridControl,搞不懂GridControl下面為什么有一堆的GridView呢?后來想想,原來GridControl是一個大容器,下面有很多視圖,那么就是操作視圖,下面是我的一些常用樣式設置。

  

            bandedGridView1.IndicatorWidth = 40; //自增列寬度
            bandedGridView1.OptionsView.ColumnAutoWidth = false; //自動調整列寬,使所有列的寬度和視圖的寬度匹配
            bandedGridView1.OptionsCustomization.AllowSort = false;//禁止用戶對數據進行排序操作
            bandedGridView1.OptionsCustomization.AllowColumnResizing = false; //禁止各列頭改變列寬
            bandedGridView1.VertScrollVisibility = ScrollVisibility.Auto; //顯示垂直滾動條
            bandedGridView1.HorzScrollVisibility = ScrollVisibility.Auto; //顯示水平滾動條
            bandedGridView1.OptionsMenu.EnableColumnMenu = false; //禁止列頭上的菜單
            bandedGridView1.OptionsMenu.EnableFooterMenu = false; //禁止頁腳上的菜單
            bandedGridView1.OptionsMenu.EnableGroupPanelMenu = false; //禁止分組面板上的菜單
            bandedGridView1.OptionsNavigation.UseTabKey = false; //不使用TAB/SHIFT+TAB移動焦點
            bandedGridView1.OptionsBehavior.Editable = false;//不允許編輯
            bandedGridView1.OptionsBehavior.ReadOnly = true;//只讀

   3.這些事件你會常用

     CustomDrawRowIndicator:DGV顯示自增行號

            e.Appearance.TextOptions.HAlignment = HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString(CultureInfo.InvariantCulture);
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle;
                }
            }

           RowCellStyle:GridView隔行變色

 bandedGridView1.Appearance.OddRow.BackColor = Color.White; // 設置奇數行顏色 // 默認也是白色 可以省略 
            bandedGridView1.OptionsView.EnableAppearanceOddRow = true; // 使能 // 和和上面綁定 同時使用有效 
            bandedGridView1.Appearance.EvenRow.BackColor = Color.FromArgb(255, 250, 205); // 設置偶數行顏色 
            bandedGridView1.OptionsView.EnableAppearanceEvenRow = true; // 使能 // 和和上面綁定 同時使用有效
            if (e.RowHandle == bandedGridView1.FocusedRowHandle) { e.Appearance.Font = new Font("宋體", 9, FontStyle.Bold); }
View Code

     FocusedRowChanged選中行改變綁定行數據到對應控件中 

if (bandedGridView1.GetFocusedDataRow() == null) return;//判斷當前選中行是否為null
            //返回值
            var columnValue= bandedGridView1.GetFocusedRowCellValue("綁定列字段名稱").ToString();

     4.添加右擊菜單一個很常用的功能

     

    添加ContextMenuStrip控件,然后自定義按鈕,可以找一些16*16的小圖標點綴一下,很不錯

       

這樣一個右鍵菜單就已經綁定成功

      5.GridControl全選和反選

     這是一個比較難的問題,這里我結合網上尋找的資料總結了一個實體類貼上源代碼

 

//------------------------------------------------------------------------------------- // All Rights Reserved , Copyright (C) 2014 , ZTO , Ltd . //-------------------------------------------------------------------------------------

using System.Drawing; using System.Windows.Forms; using DevExpress.XtraEditors.Repository; namespace ZTO.WayBill.Utilities { /// <summary>
    /// Dev GridControl 創建全選復選框 ///
    /// 修改紀錄 ///
    /// 2014-5-30 版本:1.0 YangHengLian 創建主鍵,注意命名空間的排序。 /// 
    /// 版本:1.0 ///
    /// <author>
    ///        <name>YangHengLian</name>
    ///        <date>2014-5-30</date>
    /// </author>
    /// </summary>
    public class DevControlHelper { /// <summary>
        /// 創建復選框 /// </summary>
        /// <param name="e"></param>
        /// <param name="chk"></param>
        public static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk) { RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as RepositoryItemCheckEdit; if (repositoryCheck != null) { Graphics g = e.Graphics; Rectangle r = e.Bounds; DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info; DevExpress.XtraEditors.Drawing.CheckEditPainter painter; DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args; info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo; painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter; info.EditValue = chk; 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(); } } /// <summary>
        /// 全選,反選 /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fieldName"></param>
        /// <param name="currentStatus"></param>
        /// <returns></returns>
        public static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus) { bool result = false; if (gridView != null) { gridView.ClearSorting();//禁止排序
 gridView.PostEditor(); DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info; Point pt = gridView.GridControl.PointToClient(Control.MousePosition); info = gridView.CalcHitInfo(pt); if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName) { for (int i = 0; i < gridView.RowCount; i++) { gridView.SetRowCellValue(i, fieldName, !currentStatus); } return true; } } return result; } } }
View Code

 

 

下面是使用步驟

窗體加載事件添加一些代碼

private bool _mCheckStatus; //GridControl全選,作為全局變量,默認false
        private void FrmInputBill_Load(object sender, EventArgs e) { bandedGridView1.OptionsBehavior.Editable = false; bandedGridView1.OptionsBehavior.ReadOnly = true; var col = new BandedGridColumn { FieldName = "Check", Visible = true, VisibleIndex = 0, ColumnEdit = new RepositoryItemCheckEdit() }; col.OptionsColumn.AllowEdit = true; //CheckBox可以編輯改變
            gridBand1.Columns.Insert(0, col); bandedGridView1.Click += bandedGridView1_Click; bandedGridView1.CustomDrawColumnHeader += bandedGridView1_CustomDrawColumnHeader; bandedGridView1.DataSourceChanged += bandedGridView1_DataSourceChanged;        //這里綁定數據源
 } #region GridControl支持全選事件
 
        private void bandedGridView1_Click(object sender, EventArgs e) { if (DevControlHelper.ClickGridCheckBox(this.bandedGridView1, "Check", _mCheckStatus)) { _mCheckStatus = !_mCheckStatus; } } private void bandedGridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) { if (e.Column != null && e.Column.FieldName == "Check") { e.Info.InnerElements.Clear(); e.Painter.DrawObject(e.Info); DevControlHelper.DrawCheckBox(e, _mCheckStatus); e.Handled = true; } } private void bandedGridView1_DataSourceChanged(object sender, EventArgs e) { GridColumn column = this.bandedGridView1.Columns.ColumnByFieldName("Check"); if (column != null) { column.Width = 40; column.OptionsColumn.ShowCaption = false; column.ColumnEdit = new RepositoryItemCheckEdit(); } } #endregion
全選和反選

 

 6.添加小計功能
第一步

第二步
加載完數據之后綁定

bandedGridView1.Columns["綁定字段列名"].Summary.Add(DevExpress.Data.SummaryItemType.Count, "BILL_CODE", "總計:{0}條"); //添加小計功能,在指定的列上添加一個統計對象
刪除動作修改對應的小計信息,代碼如下:
bandedGridView1.Columns["綁定字段列名"].Summary[0].DisplayFormat = string.Format("小計{0}條", bandedGridView1.RowCount);
7.固定表頭


記錄了一些GridControl的常用代碼,分享給大家,如果有興趣的可以到我的群IT(霸氣測漏)里進行提問,不僅包括GridControl,其他的控件我也研究了,運用到項目中

將來的你一定會感激現在拼命的自己


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM