Dev用于界面按选中列进行分组统计数据源(实用技巧)

如果有用U8的可以明白这个功能就是模仿他的统计功能。我不过是把他造成通用的与适应于DEV的。

(效率为6000条数据分组统计时间为3秒左右分组列过多5秒。1000条以下0.几秒,500条下0.00几秒)

前置准备 需求DevExpress.XtraGrid.Views.Grid.GridView 一个grid数据源。原生为DataGridView 。Dev重写为gridview

由于Dev的绑定entity与datatable绑定上去Gridview类型会不一致。前者导致gridview.GetDataRow()为null。后者又不能进行反射获取值

我本来想按步骤贴代码的。但是考虑到现在的程序员大多都是拿来主义。一点改动后就不能愉快的玩耍了。所以还是简洁的上代码

form1 主要是为了进行gridview列头按可sum的类型与可分组的类型进行区分展示。目的为了获取到用户自定义筛选的列。

 public partial class Form1 : Dnf.BaseObject.XtraForm.BaseForm
    {
        public DevExpress.XtraGrid.Views.Grid.GridView dgw1 { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<ColumnPropers> cpFZ = new List<ColumnPropers>();
            List<ColumnPropers> cpSum = new List<ColumnPropers>();
            if (dgw1.RowCount > 0)
            {
                DataRow GridDataRow = dgw1.GetDataRow(0);
                if (GridDataRow == null)
                {
                    var a = dgw1.GetRow(0);
                }
                foreach (GridColumn dc in dgw1.Columns)
                {
                    if (!string.IsNullOrEmpty(dc.Caption))
                    {
                        if (dc.ColumnType.FullName.Contains("System.Decimal")) { cpSum.Add(new ColumnPropers() { IsSelect = true, ColumnName = dc.Caption, ColumnFieldName = dc.FieldName }); }
                        else if (dc.ColumnType.FullName.Contains("System.Int")) { cpSum.Add(new ColumnPropers() { IsSelect = true, ColumnName = dc.Caption, ColumnFieldName = dc.FieldName }); }
                        else if (dc.ColumnType.FullName.Contains("System.Double")) { cpSum.Add(new ColumnPropers() { IsSelect = true, ColumnName = dc.Caption, ColumnFieldName = dc.FieldName }); }
                        else { cpFZ.Add(new ColumnPropers() { IsSelect = false, ColumnName = dc.Caption, ColumnFieldName = dc.FieldName }); }
                    }
                }
                //foreach (PropertyInfo pi in GridDataRow.GetType().GetProperties())
                //{

                //    if (pi == null) continue;
                //    if (pi.CanWrite == false) continue;
                //    decimal dc = 0.00m;
                //    if (pi.GetValue(GridDataRow, null) != null)
                //        if (decimal.TryParse(pi.GetValue(GridDataRow, null).ToString(), out dc)) { cpSum.Add(new ColumnPropers() { IsSelect = true, ColumnName = pi.Name }); }
                //        else { cpFZ.Add(new ColumnPropers() { IsSelect = false, ColumnName = pi.Name }); }
                //}
            }
            gridControl1.DataSource = cpFZ;
            gridControl2.DataSource = cpSum;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataRow GridDataRow = gridView1.GetDataRow(0);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            Dictionary<string, string> dicPX = new Dictionary<string, string>();
            Dictionary<string, string> dicSum = new Dictionary<string, string>();
            bool ischeck = false;
            if (GridDataRow == null)
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    var a = gridView1.GetRow(i);
                    string ColumnName = "";
                    string ColumnFieldName = "";
                    foreach (PropertyInfo pi in a.GetType().GetProperties())
                    {
                        if (pi == null) continue;
                        if (pi.CanWrite == false) continue;
                        if (pi.GetValue(a, null) == null) continue;
                        if (string.IsNullOrEmpty(pi.GetValue(a, null).ToString())) { break; }
                        if (pi.Name == "IsSelect") { if (bool.TryParse(pi.GetValue(a, null).ToString(), out ischeck)) { if (!ischeck) { break; } } else { break; } }
                        else if (pi.Name == "ColumnName")
                        {
                            ColumnName = pi.GetValue(a, null).ToString();
                        }
                        else if (pi.Name == "ColumnFieldName")
                        {
                            ColumnFieldName = pi.GetValue(a, null).ToString();

                        }
                    }
                    if (!string.IsNullOrEmpty(ColumnFieldName))
                    {
                        dic[ColumnFieldName] = ColumnName;
                        dicPX[ColumnFieldName] = ColumnName;
                    }
                }
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    var a = gridView2.GetRow(i);
                    string ColumnName = "";
                    string ColumnFieldName = "";
                    foreach (PropertyInfo pi in a.GetType().GetProperties())
                    {
                        if (pi == null) continue;
                        if (pi.CanWrite == false) continue;
                        if (pi.GetValue(a, null) == null) continue;
                        if (string.IsNullOrEmpty(pi.GetValue(a, null).ToString())) { break; }
                        if (pi.Name == "IsSelect") { if (bool.TryParse(pi.GetValue(a, null).ToString(), out ischeck)) { if (!ischeck) { break; } } else { break; } }
                        else if (pi.Name == "ColumnName")
                        {
                            ColumnName = pi.GetValue(a, null).ToString();
                        }
                        else if (pi.Name == "ColumnFieldName")
                        {
                            ColumnFieldName = pi.GetValue(a, null).ToString();
                        }
                    }
                    if (!string.IsNullOrEmpty(ColumnFieldName))
                    {
                        dic[ColumnFieldName] = ColumnName;
                        dicSum[ColumnFieldName] = ColumnName;
                    }
                }
            }
            else
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    DataRow GridDataRow1 = gridView1.GetDataRow(i);
                    if (bool.TryParse(GridDataRow1["IsSelect"].ToString(), out ischeck))
                    {
                        if (ischeck)
                        {
                            if (!string.IsNullOrEmpty(GridDataRow1["ColumnFieldName"].ToString()))
                            {
                                dic[GridDataRow1["ColumnFieldName"].ToString()] = GridDataRow1["ColumnName"].ToString();
                                dicPX[GridDataRow1["ColumnFieldName"].ToString()] = GridDataRow1["ColumnName"].ToString();
                            }
                        }
                    }
                }
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    DataRow GridDataRow1 = gridView2.GetDataRow(i);
                    if (bool.TryParse(GridDataRow1["IsSelect"].ToString(), out ischeck))
                    {
                        if (ischeck)
                        {
                            if (!string.IsNullOrEmpty(GridDataRow1["ColumnFieldName"].ToString()))
                            {
                                dicSum[GridDataRow1["ColumnFieldName"].ToString()] = GridDataRow1["ColumnName"].ToString();
                                dic[GridDataRow1["ColumnFieldName"].ToString()] = GridDataRow1["ColumnName"].ToString();
                            }
                        }
                    }
                }
            }
            using (Form2 f2 = new Form2())
            {

                f2.dgw1 = dgw1;
                f2.dic = dic;
                f2.dicPX = dicPX;
                f2.dicSum = dicSum;
                //f2.SumName = sumname.Split(‘…‘);
                //f2.FieldName1 = pfname.Split(‘…‘);
                this.Close();
                f2.ShowDialog();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            TTTTT();
            this.Close();
        }

    }

    public class ColumnPropers
    {
        public bool IsSelect { get; set; }
        public string ColumnName { get; set; }
        public string ColumnFieldName { get; set; }
    }

form2就是通过用户自定义筛选的列与用户当前操作的数据源进行分组统计。

  public Form2()
        {
            InitializeComponent();
        }
        public DevExpress.XtraGrid.Views.Grid.GridView dgw1 { get; set; }
        public string[] FieldName1 { get; set; }
        public string[] SumName { get; set; }
        /// <summary>
        /// 一个key为字段名称,value为中文名称的字典
        /// </summary>
        public Dictionary<string, string> dic { get; set; }
        public Dictionary<string, string> dicPX { get; set; }
        public Dictionary<string, string> dicSum { get; set; }
        private void Form2_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Restart();
            ToSumColumn(dgw1, FieldName1);
            stopwatch.Stop();
            Console.WriteLine("TryParse所用时间:" + stopwatch.Elapsed);
            MessageBox.Show(stopwatch.Elapsed.TotalSeconds.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dgw">gridview</param>
        /// <param name="FieldName">需展示的表头字段</param>
        /// <param name="FieldName">需展示的表头字段</param>
        public void ToSumColumn(DevExpress.XtraGrid.Views.Grid.GridView dgw, string[] FieldName)
        {

            DataGridView dgv = new DataGridView();
            DataTable dt = new DataTable();
            //构造datatable列头根据字典
            foreach (KeyValuePair<string, string> kvp in dic)
            {
                DataColumn column = new DataColumn();
                //column.DataType = ;
                column.ColumnName = kvp.Value;
                //column.Unique = true;
                dt.Columns.Add(column);
            }

            if (dgw.GetDataRow(0) == null)
            {
                dt = ReturnDTByGridView(dgw, dt, FieldName);
            }
            else
            {
                dt = ReturnDTBySqlGrid(dgw, dt, FieldName);
            }
            //gridControl2.DataSource = dt;
            gridControl1.DataSource = dt;
        }

        public DataTable ReturnDTBySqlGrid(DevExpress.XtraGrid.Views.Grid.GridView dgw, DataTable dt, string[] FieldName)
        {
            for (int i = 0; i < dgw.RowCount; i++)
            {
                DataRow dr = dt.NewRow();
                foreach (KeyValuePair<string, string> kvp in dic)
                {
                    dr[kvp.Value] = dgw.GetDataRow(i)[kvp.Key];
                }
                bool BtIsHasThisData1 = false;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    var a = dt.Rows[j][0];
                    bool BtIsHasThisData = false;
                    foreach (KeyValuePair<string, string> kvp in dicPX)
                    {
                        if (dt.Rows[j][kvp.Value].ToString() == dr[kvp.Value].ToString())
                        {
                            BtIsHasThisData = true;
                        }
                        else { BtIsHasThisData = false; break; }
                    }
                    if (BtIsHasThisData)
                    {
                        foreach (KeyValuePair<string, string> kvp in dicSum)
                        {
                            if (!string.IsNullOrEmpty(dr[kvp.Value].ToString()))
                            {
                                dt.Rows[j][kvp.Value] = Convert.ToDecimal(dt.Rows[j][kvp.Value]) + Convert.ToDecimal(dr[kvp.Value]);
                                BtIsHasThisData1 = true;
                            }
                        }
                    }
                }
                if (!BtIsHasThisData1) { dt.Rows.Add(dr); }
            }
            return dt;
        }

        /// <summary>
        /// 不是datatable类型的gridview转datatable。并且统计
        /// </summary>
        /// <param name="dgw"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        public DataTable ReturnDTByGridView(DevExpress.XtraGrid.Views.Grid.GridView dgw, DataTable dt, string[] FieldName)
        {
            //foreach (GridColumn gc in dgw.Columns)
            //{
            //    for (int i = 0; i < FieldName.Count(); i++)
            //    {
            //        if (gc.FieldName == FieldName[i])
            //        {
            //            DataColumn column = new DataColumn();
            //            //column.DataType = ;
            //            column.ColumnName = gc.FieldName;
            //            //column.Unique = true;
            //            dt.Columns.Add(column);
            //        }
            //    }
            //}
            for (int i = 0; i < dgw.RowCount; i++)
            {
                var GridDataRow = dgw.GetRow(i);
                DataRow dr = dt.NewRow();
                bool isToFlase = false;
                //循环数据源的列头:进行新DataRow赋值
                dr = ReturnDataRowBy(GridDataRow, dt, FieldName);
                bool BtIsHasThisData1 = false;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    //var a = dt.Rows[j][0];
                    bool BtIsHasThisData = false;
                    foreach (KeyValuePair<string, string> kvp in dicPX)
                    {
                        if (dt.Rows[j][kvp.Value].ToString() == dr[kvp.Value].ToString())
                        {
                            BtIsHasThisData = true;
                        }
                        else { BtIsHasThisData = false; break; }
                    }
                    if (BtIsHasThisData)
                    {
                        foreach (KeyValuePair<string, string> kvp in dicSum)
                        {
                            if (!string.IsNullOrEmpty(dr[kvp.Value].ToString()))
                            {
                                dt.Rows[j][kvp.Value] = Convert.ToDecimal(dt.Rows[j][kvp.Value]) + Convert.ToDecimal(dr[kvp.Value]);
                                BtIsHasThisData1 = true;
                            }
                        }
                    }
                }
                if (!BtIsHasThisData1) { dt.Rows.Add(dr); }
            }
            return dt;
        }
        /// <summary>
        /// 实体对象的gridview调用Grid.Rows To DataRow
        /// </summary>
        /// <param name="GridDataRow">实体对象</param>
        /// <param name="dt"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        public DataRow ReturnDataRowBy(object GridDataRow, DataTable dt, string[] FieldName)
        {
            DataRow dr = dt.NewRow();
            foreach (PropertyInfo pi in GridDataRow.GetType().GetProperties())
            {
                if (pi == null) continue;
                if (pi.CanWrite == false) continue;
                decimal dc = 0.00m;
                foreach (KeyValuePair<string, string> kvp in dic)
                {
                    if (kvp.Key == pi.Name)
                    {
                        //var pp1 = pi.GetValue(GridDataRow, null);
                        if (pi.GetValue(GridDataRow, null) != null)
                        {
                            dr[kvp.Value] = pi.GetValue(GridDataRow, null).ToString();
                        }
                    }
                }
            }
            return dr;
        }

如需要通用版的 请加我Q 236598515

时间: 2024-10-07 01:23:32

Dev用于界面按选中列进行分组统计数据源(实用技巧)的相关文章

R语言-分组统计

分组统计 1.假定有一组成绩数据,要求根据性别进行分组统计: > score    ID   score1 score2 Gender1  101 11.35321    0.9   male2  102 12.58654    0.6   male3  103 19.79380    0.9 female4  104 12.01352    0.4 female5  105 11.97081    0.4 female6  106 17.37480    0.7 female7  107 14

DEV的GridControl控件的选中列属性设置高光

设置Run Designer=>Views=> OptionsSelection下面的: EnableAppearanceFocusedCell = False,//鼠标移开,失去焦点,仍然保持其原有的样式风格 EnableAppearanceHideSelection = False//控制点击选中行是否出现白色背景的单元格. 设置背景颜色: Run Designer=>Views=>Appearance=>FocusedRow=>BackColor//设置击中行的背

Oracle(零)--&gt; 伪列和分组聚合(Oracle 开篇,比较初级啦~)

   写在开篇~    不知不觉已然到了开学的时候,毕竟这个大三连暑假还不知道是什么就上岗了,看着周围同学秀的简直飞起 =.= 扯淡扯多了,再扯一句..大三的时候学的 Oracle ,但是现在一想竟然不觉得有何内容,而且触发器,存储过程老师也没有教过,自己也没有看过,实在...毕竟华为工程师说的那句话还是依稀在眼前..不管怎么样,先搞明白是何物,以后才在项目中知道该如何去使用.本流程基本按学习过程中 PPT 来走的,而再往前的东西概念太多,大家又不是很喜欢字,都爱看图嘛~视频就算了,太刺激了都受

[Easyui - Grid]为easyui的datagrid、treegrid增加表头菜单,用于显示或隐藏列

为easyui的datagrid.treegrid增加表头菜单,用于显示或隐藏列 /** * @author 孙宇 * * @requires jQuery,EasyUI * * 为datagrid.treegrid增加表头菜单,用于显示或隐藏列,注意:冻结列不在此菜单中 */ var createGridHeaderContextMenu = function(e, field) { e.preventDefault(); var grid = $(this);/* grid本身 */ var

【案例分享】电力设备生产数据的多层分组统计报表实现

多层分组统计报表即按照不同的数据字段,形成多级分组,并分层级进行合计. 传统报表的实现方式大多基于 Table 控件,虽然可实现多个分组功能,但在报表显示方面有限制,只能呈现上下级的分组,而现代的复杂报表的需求,通常是左右级嵌套,有时甚至要求相同内容的单元格合并,使用 Table 控件,有太多的局限,有了矩表控件,通过简单的拖拽就能轻松实现多层分组报表,不管有多少个分组和分组小计都能简单解决. 下面会通过一个具体的案例,使用葡萄城报表中的矩表控件来讲解实现多层分组统计报表. (一)原始数据 (二

Oracle按不同时间分组统计

Oracle按不同时间分组统计 Oracle按不同时间分组统计的sql 如下表table1: 日期(exportDate) 数量(amount) -------------- ----------- 14-2月 -08 20 10-3月 -08 2 14-4月 -08 6 14-6月 -08 75 24-10月-09 23 14-11月-09 45 04-8月 -10 5 04-9月 -10 44 04-10月-10 88 注意:为了显示更直观,如下查询已皆按相应分组排序 1.按年份分组 sel

分组统计查询(学习笔记)

查询出公司每个月支出的工资总和 --查询出公司每个月支出的工资总和 SELECT SUM(sal) FROM emp; 查询出公司的最高工资,最低工资和平均工资 --查询出公司的最高工资,最低工资和平均工资 SELECT MAX(sal),MIN(sal),ROUND(AVG(sal),2) FROM emp; 统计出公司最早雇佣和最晚雇佣的雇佣日期 --统计出公司最早雇佣和最晚雇佣的雇佣日期 SELECT MIN(hiredate) 最早雇佣日期 , MAX(hiredate) 最晚雇佣日期

010.简单查询、分组统计查询、多表连接查询(sql实例)

-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SELECT UNION --SELECT 'HAHA',1,32--UNION ALL --全部显示/显示重复数据 即使集合相同--UNION---------将查询的两个结果集合并.结构必须一致 -->常见面试题 --SELECT 'HEHE',2,33------将查询结果添加到列表中(子查询)IN

003.分组统计查询和表连接查询

--分组统计查询 group by having 1 select 分组字段 ,聚合函数 2 having 后常跟聚合函数,也可以跟分组字段 3 where 后不可以直接跟聚合函数 4 where(筛选行) -group by (分组) -having (筛选组) --表连接查询 1 笛卡尔积 (交叉连接 cross join) a*b A) SELECT * FROM A,B B) SELECT * FROM A CROSS JOIN B 2 内连接 原理: 将两个表中关联字段相等的行查询出来