DataGridView控件实现行折叠效果

本文介绍如何使用 C# 在 DataGridView 控件中实现自定义分组功能,包括创建自定义 DataGridViewGroupCell 类来实现组节点的绘制和交互,以及通过实例演示如何添加和展示分组数据。

一 重写表格

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DataGrideViewControl
{
   
   
    public class DataGridViewGroupCell : DataGridViewTextBoxCell
    {
   
   
        #region Variant

        /// <summary>  
        /// 标示的宽度  
        /// </summary>  
        const int PLUS_WIDTH = 24;

        /// <summary>  
        /// 标示的区域  
        /// </summary>  
        Rectangle groupPlusRect;

        #endregion

        #region Init

        public DataGridViewGroupCell()
        {
   
   
            groupLevel = 1;
        }

        #endregion

        #region Property

        int groupLevel;

        /// <summary>  
        /// 组级别(以1开始)  
        /// </summary>  
        public int GroupLevel
        {
   
   
            get {
   
    return groupLevel; }
            set {
   
    groupLevel = value; }
        }

        DataGridViewGroupCell parentCell;

        /// <summary>  
        /// 父节点  
        /// </summary>  
        public DataGridViewGroupCell ParentCell
        {
   
   
            get
            {
   
   
                return parentCell;
            }
            set
            {
   
   
                if (value == null)
                    throw new NullReferenceException("父节点不可为空");
                if (!(value is DataGridViewGroupCell))
                    throw new ArgumentException("父节点必须为 DataGridViewGroupCell 类型");

                parentCell = value;
                parentCell.AddChildCell(this);
            }
        }

        private bool collapsed;

        /// <summary>  
        /// 是否收起  
        /// </summary>  
        public bool Collapsed
        {
   
   
            get {
   
    return collapsed; }
        }

        private List<DataGridViewGroupCell> childCells = null;

        /// <summary>  
        /// 所有的子结点  
        /// </summary>  
        public DataGridViewGroupCell[] ChildCells
        {
   
   
            get
            {
   
   
                if (childCells == null)
                    return null;
                return childCells.ToArray();
            }
        }

        /// <summary>  
        /// 取得组标示(有+或-号的框)的区域  
        /// </summary>  
        public Rectangle GroupPlusRect
        {
   
   
            get
            {
   
   
                return groupPlusRect;
            }
        }

        bool bPaint = true;

        /// <summary>  
        /// 是否重绘  
        /// </summary>  
        public bool BPaint
        {
   
   
            get {
   
    return bPaint; }
            set {
   
    bPaint = value; }
        }

        #endregion

        #region 添加子节点

        /// <summary>  
        /// 添加子结点  
        /// </summary>  
        /// <param name="cell"></param>  
        /// <returns></returns>  
        public int AddChildCell(DataGridViewGroupCell cell)
        {
   
   
            return AddChildCellRange(new DataGridViewGroupCell[] {
   
    cell });
        }

        public int AddChildCellRange(DataGridViewGroupCell[] cells)
        {
   
   
            bool needRedraw = false;
            if (childCells == null)
            {
   
   
                //需要画一个加号  
                childCells = new List<DataGridViewGroupCell>();
                needRedraw = true;
            }
            foreach (DataGridViewGroupCell cell in cells)
        
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值