dataGridView分组

最近项目用到此功能,找了一些资料 ,并自己做了个测试。上代码:

定义一个类:

class Student
    {
        public string Name { get; set; }


        public string ClassName { get; set; }


        public int Age { get; set; }


        public string StuNo { get; set; }


    }

定义全局变量:

Hashtable ht;//存储父节点状态

程序初始化或者加载后向类中添加数据:

List<Student> list = new List<Student>()
           {
           new Student{Name="aaa",ClassName="a",Age=20,StuNo="a001"},
           new Student{Name="bbb",ClassName="a",Age=18,StuNo="a003"},
           new Student{Name="ccc",ClassName="b",Age=20,StuNo="b001"},
           new Student{Name="ddd",ClassName="b",Age=20,StuNo="b045"},
           new Student{Name="eee",ClassName="c",Age=20,StuNo="c001"},
           new Student{Name="fff",ClassName="c",Age=20,StuNo="c008"},
           new Student{Name="ggg",ClassName="c",Age=20,StuNo="c050"},
           new Student{Name="hhh",ClassName="c",Age=20,StuNo="c007"},
           new Student{Name="hjj",ClassName="d",Age=22,StuNo="d007"}
           };

初始化的时候动态添加列名:

            DataGridViewTextBoxColumn txtKey = new DataGridViewTextBoxColumn();
            txtKey.Name = "txtKey";
            txtKey.HeaderText = "班级";
            this.dataGridView1.Columns.Add(txtKey);


            DataGridViewTextBoxColumn txtValue = new DataGridViewTextBoxColumn();
            txtValue.Name = "txtValue";
            txtValue.HeaderText = "姓名";
            this.dataGridView1.Columns.Add(txtValue);


            DataGridViewTextBoxColumn txtStatus = new DataGridViewTextBoxColumn();
            txtStatus.Name = "txtStatus";
            txtStatus.HeaderText = "年龄";
            this.dataGridView1.Columns.Add(txtStatus);


            DataGridViewTextBoxColumn txtStudentID = new DataGridViewTextBoxColumn();
            txtStudentID.Name = "txtStudentID";
            txtStudentID.HeaderText = "学号";
            this.dataGridView1.Columns.Add(txtStudentID);


            DataGridViewTextBoxColumn txtStuKey = new DataGridViewTextBoxColumn();
            txtStuKey.Name = "txtStuKey";
            txtStuKey.Visible = false;
            txtStuKey.HeaderText = "班级";
            this.dataGridView1.Columns.Add(txtStuKey);

将List转换为Dictionary:

Dictionary<string, List<Student>> stuGroup = new Dictionary<string, List<Student>>();


            foreach (Student item in list)
            {
                if (!stuGroup.Keys.Contains(item.ClassName))
                {
                    stuGroup.Add(item.ClassName, new List<Student>());
                }
                stuGroup[item.ClassName].Add(item);
            }

循环向dataGridView中添加数据:

String sKey = String.Empty;
            String sClsName = String.Empty;
            String sValue = String.Empty;
            String sStatus = String.Empty;
            String sStuId = String.Empty;
            int nRow = 0;
            int nChild = 1;
            foreach (KeyValuePair<string, List<Student>> item in stuGroup)
            {
                if (item.Key != null)
                {
                    if (nRow>1)
                    {
                        nRow += 1;
                    }
                    DataGridViewRow dbRow = new DataGridViewRow();
                    this.dataGridView1.Rows.Add(dbRow);
                    this.ht = new Hashtable();
                    sKey = item.Key;
                    this.ht.Add(item.Key, false);                    
                    this.dataGridView1.Rows[nRow].Cells[0].Value = item.Key;//父级显示
                    this.dataGridView1.Rows[nRow].Cells[4].Value = item.Key;//父级,注意这里 避免dataGridView1_RowPostPaint事件中强转出错
                    foreach (Student stu in item.Value)
                    {
                        if (item.Value != null || item.Value.Count != 0)
                        {
                            DataGridViewRow childRow = new DataGridViewRow();
                            this.dataGridView1.Rows.Add(childRow);
                            sValue = stu.Name;
                            sStatus = stu.Age.ToString();
                            sStuId = stu.StuNo;
                            sClsName = stu.ClassName;
                            this.dataGridView1.Rows[nRow+1].Cells[1].Value = sValue;//姓名
                            this.dataGridView1.Rows[nRow+1].Cells[2].Value = sStatus;//年龄
                            this.dataGridView1.Rows[nRow+1].Cells[3].Value = sStuId;//学号
                            this.dataGridView1.Rows[nRow+1].Cells[4].Value = sClsName;//父级,注意这里 避免dataGridView1_RowPostPaint事件中强转出错
                        }
                        //注意这个变量必须放在子循环中
                        nRow++; 
                    }
                }
            }

添加dataGridView的RowHeaderMouseClick事件:

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {           
            //通过Hashtable比对数据
            String sParent = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            String sName = "";
            if (null == this.dataGridView1.Rows[e.RowIndex].Cells[0].Value)
            {
                sName = "0";
            }
            else
            {
                sName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            
            if (sParent != null)
            {
                ht[sName] = !Convert.ToBoolean(ht[sName]);
                //重绘之前要显示所有行,否则隐藏行不会再绘制
                foreach (DataGridViewRow gr in dataGridView1.Rows)
                {
                    if (false == gr.Visible)
                    {
                        gr.Visible = true;
                    }
                }
                dataGridView1.Refresh();
                dataGridView1.EndEdit();
            }
        }

添加dataGridView的RowPostPaint事件:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (this.dataGridView1.Rows[e.RowIndex].Cells[0].Value != null)//当前行为父节点时绘制图标
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X + this.dataGridView1.RowHeadersWidth - 20
                                                        , e.RowBounds.Location.Y + 4, 14, 14);
                Image img = Convert.ToBoolean(ht[this.dataGridView1.Rows[e.RowIndex].Cells[0].Value])
                            ? Properties.Resources.minus : Properties.Resources.plus;
                e.Graphics.DrawImage(img, rectangle);
            }
            else
            {
                this.dataGridView1.Rows[e.RowIndex].Visible = Convert.ToBoolean(ht[this.dataGridView1.Rows[e.RowIndex].Cells[4].Value]);
                //this.dataGridView1.Rows[e.RowIndex].Visible = false;
            }
        }


没有过多的注释。将代码复制执行就可以了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
与6.0版相比,控件6.01版的主要更改如下: 1、解决6.0版在XP及Windows Server 2008等操作系统下打印预览及打印不正确的问题(第1页正常,其他各页全部是空白)。 控件主要功能: 1、强大的DataGridView打印功能,不仅可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印)打印DGV表格,基本上能完全按DGV控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,基本上做到了所见即所得的打印。 2、报表设计功能。报表模板设计组件EasyReport可以设计普通报表、分组报表、套打模板等,以DataGridView为数据源。控件的位置以毫米为计量单位,定位准确,很适合套打单据设计。 3、图表打印功能。5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件中打印出来,也可以在Graphics对象中显示。 4、文本打印输出功能,控件提供多个文本打印重载函数,打印文本时,如果需要,控件会自动换行和换页打印输出。还增加了以指定行间距及字符间距打印文本的功能,可以用固定行距,也可以用单倍或多倍行距打印文本。 5、绘图功能,基本上.NET的GDI+的绘图函数(如直线、矩形、路径、多边形、曲线等)都有,只有个别函数的名称有点区别。 6、支持同一文档多种版面格式打印(类似于Word中的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边距),只需要在新增一页时在NewPage方法中指定要使用的页面格式即可,使用非常简单。 7、报表文件保存功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 8、数据导出功能,可以将DataGridView导出为Excel\PDF\RTF\HTML文件,5.7版控件使用开源的NPOI导出Excel,速度非常快,效果非常好。 9、强大的容器控件打印功能(DrawPanel函数)。借助该函数,您只需要在您的容器控件中设计好要打印的内容及打印内容的相对位置,控件轻松帮你打印出来(如果超过一页,控件会自动换页续打)。 10、5.6版新增的SimpleReport组件允许您在一个方案文件中管理多个打印方案,在打印预览时能自由在各个打印方案之间切换。 11、RichTextBox控件的RTF文本打印功能。 12、页眉页脚中既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 13、多表头(跨行跨列的复杂表头)打印功能,多表头组件支持多表头显示与打印、单元格内容的合并显示、打印与导出。 14、自定义纸张支持功能。 15、直接打印窗口中的TreeView控件功能。 16、打印窗口中的ListView功能。 17、斜线表头打印功能。 18、各种条形码(包括二维码)打印功能。 19、5.7版控件增加了使用开源的NPOI从Excel文件(支持2003与2007格式)中导入数据到DataGridView的功能,以及DataGridView的复制与粘贴功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值