1. 程式人生 > >gridcontrol 根據某一列數據來控制其他列合並

gridcontrol 根據某一列數據來控制其他列合並

lte index 五行 substring export private 打印 save summary

首先需要屬性欄中設置這一列可以合並,再在CellMerge方法中寫

 private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            if (e.Column.FieldName == "題目" || e.Column.FieldName == "分值" )
            {
                string valueFirstColumn1 = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle1, gridView1.Columns["三級標題"]));
                string valueFirstColumn2 = Convert.ToString(gridView1.GetRowCellValue(e.RowHandle2, gridView1.Columns["三級標題"]));
                if(valueFirstColumn1 == valueFirstColumn2 )
                {
                    e.Merge =true ;
                    if (e.RowHandle2 > maxhandle && e.Column.FieldName == "題目")
                    {
                        string lastValue = gridView1.GetRowCellValue(e.RowHandle1, gridView1.Columns["題目"]).ToString();
                        string repeatValue = gridView1.GetRowCellValue(e.RowHandle2, gridView1.Columns["題目"]).ToString();
                        gridView1.SetRowCellValue(e.RowHandle1, gridView1.Columns["題目"], lastValue + "\n" + repeatValue);
                        maxhandle = e.RowHandle2;
                        int fenzhiValue= Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle1, gridView1.Columns["分值"]));
                        int fenzhiRepeatValue= Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle2, gridView1.Columns["分值"]));
                        gridView1.SetRowCellValue(e.RowHandle1, gridView1.Columns["分值"], fenzhiValue + fenzhiRepeatValue);

                    }
                  
                }
                e.Handled = true;
            }
         
        }

  目的是根據三級標題這一列如果有連續相同的數據,就合並題目列、分值列,並且把合並的那幾行內容都顯示出來而不是只顯示合並的第一行。

cellmerge函數中if(valueFirstColumn1 == valueFirstColumn2 )裏面最開始的執行順序是,從行號為0開始如果前兩行相等,e.RowHandle1和e.RowHandle2分別是0、1,如果前三行相等,接下來就是0、2,直到所有連續的相同的檢索完,然後第四行、第五行相等,接下來就是3、4...............每次判斷兩行。不知道為啥最後又重新檢索了一遍?,為了實現合並時文字不丟失/數字合並時自動相加,用了一個maxhandle做限制來保證它一直按這個順序進行。

然而運行時form.ShowDialog();會報無參錯誤,難道是這個事件有什麽本身有什麽問題?反正代碼沒查出來問題,只有一個奇怪現象,進去之後打印或者導出一遍就不會報錯,能實現功能最好,原因真找不著。然後加載之後就設置導出excel一遍,好了。。。。

   /// <summary>
        /// 導出excel臨時文件
        /// </summary>
        /// <param name="panels"></param>
        private void ExportTest(params IPrintable[] panels)
        {
            SaveFileDialog saveFileDialog 
= new SaveFileDialog(); saveFileDialog.FileName = "gridtest"; saveFileDialog.Title = "導出Excel"; saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls"; string FileName ="C:/gridtest.xlsx"; PrintingSystem ps = new PrintingSystem(); CompositeLink link = new CompositeLink(ps); ps.Links.Add(link); foreach (IPrintable panel in panels) { link.Links.Add(CreatePrintableLink(panel)); } link.Landscape = true; try { int count = 1; //在重復名稱後加(序號) while (File.Exists(FileName)) { if (FileName.Contains(").")) { int start = FileName.LastIndexOf("("); int end = FileName.LastIndexOf(").") - FileName.LastIndexOf("(") + 2; FileName = FileName.Replace(FileName.Substring(start, end), string.Format("({0}).", count)); } else { FileName = FileName.Replace(".", string.Format("({0}).", count)); } count++; } //MessageBox.Show(FileName); if (FileName.LastIndexOf(".xlsx") >= FileName.Length - 5) { XlsxExportOptions options = new XlsxExportOptions(); link.ExportToXlsx(FileName, options); } else { XlsExportOptions options = new XlsExportOptions(); link.ExportToXls(FileName, options); } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message); } }

gridcontrol 根據某一列數據來控制其他列合並