方法1:控件自带
进入Run Designer:
① 设置属性:Views-Options-OptionsView-ShowFooter=true
②对于需要值操作的列进行操作:SummaryItem-SummaryType-Sum(可以设置sum,max,min,等)
方法2:重写方法
//要操作的表 dt; //添加显示合计的一行 if (dt.Rows.Count > 0) { DataRow dr = dt.NewRow(); dr[m_PrimarColumNm] = "合计"; SetSum(ref dr, dt, “需要合计的列名”); dt.Rows.Add(dr); } /// <summary> /// 统计表格列的总和(对于求和的列必须为数字类型) /// </summary> /// <param name="dr">显示合计的一行</param> /// <param name="dt">要操作的表格</param> /// <param name="colunNm">要求和的列</param> private void SetSum(ref DataRow dr, DataTable dt, string colunNm) { try { dr[colunNm] = "0"; foreach (DataRow item in dt.Rows) { dr[colunNm] = (Convert.ToInt32(dr[colunNm].ToString()) + Convert.ToInt32(item[colunNm].ToString())).ToString(); } } catch (Exception ex) { throw ex; } }
方法3:没实现的方法
//gridView1.OptionsView.ShowFooter = true; //gridView1.GroupSummary.Clear(); ///方法1 //gridView1.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "列名", 表格列, "组计:{0}"); ///方法2 //GridGroupSummaryItem item1 = new GridGroupSummaryItem(); //item1.FieldName = "s1"; //item1.SummaryType = DevExpress.Data.SummaryItemType.Sum; //item1.DisplayFormat = "";//格式 //item1.ShowInGroupColumnFooter = s1; //item1.ShowInGroupColumnFooterName = "s1"; //这句就是给groupcolumnfooter加上 //gridView1.GroupSummary.Add(item1); ///方法3(设计器方法) this.s1.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Sum)});