欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DevExpress GridView设置自定义分组

程序员文章站 2022-04-21 11:59:26
...

效果图

拖拽orderPrice这一列到黄色区域部分,可以出现图二的效果,分组的规则是orderPrice按照0-10,11-20.....来分组的

DevExpress GridView设置自定义分组

DevExpress GridView设置自定义分组

实现:

第一步:添加一个GridControl,设置数据源,为GridView添加CustomColumnGroup事件

 private void gridView1_CustomColumnGroup(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
        {
            //orderPrice是GridView的FieldName
            if (e.Column != null && e.Column.FieldName == "orderPrice")
            {
                double x = Math.Floor(Convert.ToDouble(e.Value1) / 10);
                double y = Math.Floor(Convert.ToDouble(e.Value2) / 10);
                int res = System.Collections.Comparer.Default.Compare(x, y);
                if (x > 9 && y > 9) res = 0;
                e.Result = res;
                e.Handled = true;
            }
        }

第二步 添加GridView的CustomDrawGroupRow事件

 private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo info = e.Info as GridGroupRowInfo;
            if (info == null) return;
            if (info.Column.FieldName == "orderPrice") {
                string interval = IntervalByValue(gridView1.GetGroupRowValue(info.RowHandle));
                string sumText = gridView1.GetGroupSummaryText(info.RowHandle);
                info.GroupText = string.Format("order Price:{0}{1}", interval, sumText);
            }
        }

        private static string IntervalByValue(object val)
        {
            double d = Math.Floor(Convert.ToDouble(val) / 10);
            string ret;
            if (d > 9)
                ret = string.Format(">={0:c}", 100);
            else
                ret=string.Format("{0:c}-{1:c}",d*10,(d+1)*10);
            return ret;

        }