對Dev的GridControl/GridView控件進行分組並展開操做

今天在模塊編寫中碰到了對錶格的分組,特地在這裏把它記錄下來。spa

1、背景:Dev14.1.3,GridControl,.NET4.0+C#設計

2、過程3d

一、GridControl設計code

一共添加4列:在下面主要是對第一和第二列進行操做,根據第一列進行分組,並對第二列進行縱向單元格合併操做;orm

二、最簡單的分組對象

僅僅根據「離線要素圖層」列展現分組結果,分組標題默認使用Dev自帶的設置,代碼以下:blog

private void SetGrouping()
        {
            //處理數據源
            PageCtrl_Second();
            MergeDataTables(DsParallel);
            DataColumn dc = new DataColumn("select", typeof(int));
            dc.DefaultValue = 1;
            dtParallel1.Columns.Add(dc);
 
            //將DataTable列綁定到GridControl上
            gc_ClnOffLine.FieldName = dtParallel1.Columns["FeaCls_OffLine"].ColumnName;
            gc_ClnStationseries.FieldName = dtParallel1.Columns["Ss_ID"].ColumnName;
            gc_ClnSelect.FieldName = dtParallel1.Columns["select"].ColumnName;
            gc_ClnPipelineName.FieldName = dtParallel1.Columns["PipeLineName"].ColumnName;
            gridView1.OptionsView.AllowCellMerge = true;
            gc_ClnOffLine.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.True;
            gc_ClnSelect.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gc_ClnStationseries.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
 
            //分組
            gc_ClnOffLine.GroupIndex = 0;//設置分組列
 
            gridView1.OptionsBehavior.AutoExpandAllGroups = true;//展開全部分組
            gridControl1.DataSource = dtParallel1;
        }

其中真正起到做用的就是設置分組列的那一行,也就是設置某一列的GroupIndex(該值默認均爲-1,即不分組);這是最低的分組顯示要求,結果以下圖:將「離線要素圖層」那一列做爲分組列,而後系統默認的顯示方式就是[列名+值]事件

固然也能夠經過GridControl的事件從新制定分組的名稱,須要使用CustomDrawGroupRow事件,實現代碼以下:ip

    private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
    {
        GridGroupRowInfo pGridGroupRowInfo = e.Info as GridGroupRowInfo;
        string strTransfer = string.Format("OffLineFeature:{0}",pGridGroupRowInfo.GroupValueText);
        pGridGroupRowInfo.GroupText = strTransfer;
        //注:GroupValueText的值表明的是這個分組單元格內的值;GroupText的值是最後展現在分組行中的內容
    }

效果如圖所示:,分組行展現的內容已經修改成自定義的了。string

三、分組名稱+字段統計

有時候,咱們除了須要展現分組的名稱以外,還想對每個分組中的數據行數進行統計,此時就須要添加一個GridGroupSummaryItem對象到gridview中,並設置他的DisplayFormat的值和SummaryType的值,具體的值的內容能夠在dev代碼提示中查看,具體實現以下:

      private void SetGrouping()
      {
            //處理數據源
            PageCtrl_Second();
            MergeDataTables(DsParallel);
            DataColumn dc = new DataColumn("select", typeof(int));
            dc.DefaultValue = 1;
            dtParallel1.Columns.Add(dc);
             
            //將DataTable列綁定到GridControl上
            gc_ClnOffLine.FieldName = dtParallel1.Columns["FeaCls_OffLine"].ColumnName;
            gc_ClnStationseries.FieldName = dtParallel1.Columns["Ss_ID"].ColumnName;
            gc_ClnSelect.FieldName = dtParallel1.Columns["select"].ColumnName;
            gc_ClnPipelineName.FieldName = dtParallel1.Columns["PipeLineName"].ColumnName;
            gridView1.OptionsView.AllowCellMerge = true;
            gc_ClnOffLine.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.True;
            gc_ClnSelect.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gc_ClnStationseries.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
             
            //分組
            gc_ClnOffLine.GroupIndex = 0;//設置分組列
            DevExpress.XtraGrid.GridGroupSummaryItem item = new DevExpress.XtraGrid.GridGroupSummaryItem();
            item.DisplayFormat = "並行站列數量:{0}";//默認"{0}: [#image]{1} {2}"
            item.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.GroupSummary.Add(item);
             
             
            gridView1.OptionsBehavior.AutoExpandAllGroups = true;//展開全部分組
            gridControl1.DataSource = dtParallel1;
      }

其實現效果以下圖所示,在最初的[列名+值]後面加上了咱們添加進來的GridGroupSummaryItem所指定的值;

擴展:這裏我只是添加了一個GridGroupSummaryItem,可是Dev還能夠連續添加多個GridGroupSummaryItem到GridView中,多個item之間會用【,】分隔開。

四、總結

  這裏的分組我只列出來這兩種:①直接展現【列名+值】;②增長一個GridGroupSummaryItem顯示相應的統計。

相關文章
相關標籤/搜索