【WinForm+DevExpress】GridView设置自定义分组

本文介绍如何在DevExpress GridView中实现按orderPrice字段进行自定义区间分组,并展示具体代码实现细节。

效果图

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

实现:

第一步:添加一个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;

        }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值