首先需要属性栏中设置这一列可以合并,再在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); } }
原文地址:https://www.cnblogs.com/chenxiya/p/10289078.html
时间: 2024-10-12 11:57:07