winform中可以折叠的datagridview,自定义控件

winform中可以折叠的datagridview,自定义控件。

目前实现了两层折叠,可以进一步拓展。

public partial class CollDataGridView : DataGridView
    {
        public CollDataGridView()
        {
            InitializeComponent();
        }

        public CollDataGridView(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            this.RowHeadersVisible = true;
            this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.ImageWidth = 15;
            this.ImageHeight = 15;
            this.RowHeadersDefaultCellStyle.Padding = new Padding(this.RowHeadersWidth);
            this.CurrentCellDirtyStateChanged += CollDataGridView_CurrentCellDirtyStateChanged;
        }

        private void CollDataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.IsCurrentCellDirty)
            {
                this.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        #region property
        /// <summary>
        /// 行首图片宽度
        /// </summary>
        [
        Category("CollDataGridViewProperties"),
        Description("行首图片宽度"),
        Bindable(true)
        ]
        public int ImageWidth { get; set; }

        /// <summary>
        /// 行首图片高度
        /// </summary>
        [
        Category("CollDataGridViewProperties"),
        Description("行首图片高度"),
        Bindable(true)
        ]
        public int ImageHeight { get; set; }

        /// <summary>
        /// 行首收缩图标
        /// </summary>
        [
        Category("CollDataGridViewProperties"),
        Description("行首收缩图标"),
        Bindable(true)
        ]
        public Image ImgExpand { get; set; }

        /// <summary>
        /// 行首展开图片
        /// </summary>
        [
        Category("CollDataGridViewProperties"),
        Description("行首展开图片"),
        Bindable(true)
        ]
        public Image ImgCollapse { get; set; }
        #endregion

        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);

            DataGridViewRow row = this.Rows[e.RowIndex];
            Rectangle rectBack = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.RowHeadersWidth, e.RowBounds.Height - 1);

            Rectangle rectLineBottom = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y + e.RowBounds.Height - 1, this.RowHeadersWidth, 1);

            if (row 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值