设为首页 加入收藏

TOP

DataGridView带图标的单元格实现(一)
2017-10-13 10:38:19 】 浏览:8033
Tags:DataGridView 标的 单元 实现


目的:

扩展 C# WinForm 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出。

这里使用的方法是:扩展 表格的列 对象:DataGridViewColumn。

1.创建类:DataGridViewDecimalCheckCell.cs

    public class DataGridViewDecimalCheckCell : DataGridViewTextBoxCell
    {
        private bool checkMaxValue = false;
        private bool checkMinValue = false;
        private decimal maxValue = 0;
        private decimal minValue = 0;

        public decimal MaxValue
        {
            get { return maxValue; }
            internal set { maxValue = value; }
        }

        public decimal MinValue
        {
            get { return minValue; }
            internal set { minValue = value; }
        }

        public bool CheckMaxValue
        {
            get { return checkMaxValue; }
            internal set { checkMaxValue = value; }
        }
        
        public bool CheckMinValue
        {
            get { return checkMinValue; }
            internal set
            {
                checkMinValue = value;
            }
        }


        public override object Clone()
        {
            DataGridViewDecimalCheckCell c = base.Clone() as DataGridViewDecimalCheckCell;
            c.checkMaxValue = this.checkMaxValue;
            c.checkMinValue = this.checkMinValue;
            c.maxValue = this.maxValue;
            c.minValue = this.minValue;
            return c;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds,
            Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
            object value, object formattedValue, string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            // Paint the base content
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
              value, formattedValue, errorText, cellStyle,
              advancedBorderStyle, paintParts);

            // 上下界限溢出判断
            if (rowIndex < 0 || this.OwningRow.IsNewRow) // 行序号不为-1,且不是新记录行
                return;
            if (value == null) return;
            if (value.GetType() == typeof(DBNull)) return;

            decimal vCurValue= Convert.ToDecimal(value);

            bool overValue = false;
            Image img = null;
            if (checkMaxValue)
            {
                overValue = vCurValue > maxValue;
                img = VsTest.Properties.Resources.Undo;
            }
            if (checkMinValue && !overValue)
            {
                overValue = vCurValue < minValue;
                img = VsTest.Properties.Resources.Redo;
            }

            // 将图片绘制在 数值文本后面
            if (overValue && img != null)
            {
                var vSize = graphics.MeasureString(vCurValue.ToString(), cellStyle.Font);

                System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
                graphics.SetClip(cellBounds);
                graphics.DrawImageUnscaled(img, new Point(cellBounds.Location.X + (int)vSize.Width, cellBounds.Location.Y));
                graphics.EndContainer(container);
            }
        }

        protected override bool SetValue(int rowIndex, object value)
        {
            if (rowIndex >= 0)
            {
                try
                {
                    decimal vdeci = Convert.ToDecimal(value);  // 筛选非数字
                    base.ErrorText = string.Empty;
                }
                catch (Exception ex)
                {
                    base.ErrorText = "输入错误" + ex.Message;
                    return false;
                }
            }
            return base.SetValue(rowIndex, value);
        }

        
    }

2.创建类:DataGridViewDecimalCheckColumn.cs

    public class  DataGridViewDecimalCheckColumn : DataGridViewColumn
    {
        private bool checkMaxValue = false;
        private bool checkMinValue = false;
        private decimal maxValue = 0;
        private decimal minValue = 0;

        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                maxValue = value;
                (base.CellTemplate as DataGridViewDecimalCheckCell).MaxValue = value;
            }
        }

        public decimal MinValue
        {
            get { return minValue; }
            set
            {
                minValue = value;
                (base.CellTemplate as DataGridViewDecimalCheckCell).MinValue = value;
            }
        }

        /// <summary>
        /// 是否对值上界限进行检查,与MaxValue配合使用
        /// </summ
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇比较和排序(IComparable和ICompa.. 下一篇C# MVC框架初学者

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目