c# winform datagridview单元格合并

本文介绍了一种在C# Winform中实现DataGridView单元格合并的方法,包括结构体、自定义DataGridView列和单元格类,以及用于合并的静态类方法。通过这些自定义组件和操作,可以实现数据网格的单元格区域合并和动态调整。
摘要由CSDN通过智能技术生成

效果

 

 

/// <summary>
    /// DataGridView 单元格合并信息
    /// </summary>
    public struct DataGridViewMergeCellRegion
    {

        public int FromRowIndex { get; set; }

        public int FromColIndex { get; set; }

        public int ToRowIndex { get; set; }

        public int ToColIndex { get; set; }
    }

 

 

 public class DataGridViewMergeColumn : DataGridViewColumn
    {

        public DataGridViewMergeColumn()
            : base(new DataGridViewMergeCell())
        {

        }

        public DataGridViewMergeColumn(DataGridViewCell cell)
            : base(cell)
        {

        }

        /// <summary>
        /// 是否是进度
        /// </summary>
        public bool IsJingDu { get; set; }
    }

    /// <summary>
    /// 如果列隐藏会有bug
    /// </summary>
    public class DataGridViewMergeCell : DataGridViewCell
    {


        int GetMergeWidth(bool all, DataGridViewMergeCellRegion reg)
        {

            int w = 0, lindex = reg.FromColIndex, lend = all ? reg.ToColIndex + 1 : this.ColumnIndex;
            int rowindex = this.RowIndex;
            DataGridViewCell cell;
            while (lindex < lend)
            {
                cell = this.DataGridView.Rows[rowindex].Cells[lindex];
                w += cell.Size.Width;
                lindex += 1;
            }
            return w;
        }
        int GetMergeHeight(bool all, DataGridViewMergeCellRegion reg)
        {

            int w = 0, lindex = reg.FromRowIndex, lend = all ? reg.ToRowIndex + 1 : this.RowIndex;
            int colindex = this.ColumnIndex;
            DataGridViewCell cell;
            while (lindex < lend)
            {
                cell = this.DataGridView.Rows[lindex].Cells[colindex];
                w += cell.Size.Height;
                lindex += 1;
            }
            return w;
        }


        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            if (value == null || value == DBNull.Value || string.IsNullOrEmpty(cellStyle.Format))
            {
                if (value == null || value == DBNull.Value)
                    return "";
                return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
            }
            return String.Format("{0:" + cellStyle.Format + "}", value);
        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formatted

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值