• 重绘DataGridView的DataGridViewCheckBoxCell控件


    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有

    的是不支持的然后我就想着重写个 DataGridViewCheckBoxTextCell
    下面是源码:(如有疑问可以加源码分享群 81582487 来问我)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;
    using System.Drawing;

    namespace WindowsDemo
    {
        public class DataGridViewCheckBoxTestCell : DataGridViewCheckBoxCell
        {

            public DataGridViewCheckBoxTestCell()
                : base()
            {

            }

            public CheckBoxState CheckBoxTestState { get; set; }
            /// <summary>
            /// 单元格边框颜色
            /// </summary>
            private Color CellBorderColor { get { return Color.FromArgb(172, 168, 153); } }

            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)
            {
                var check = (Boolean)value;
                CheckBoxTestState = check ? CheckBoxState.CheckedNormal :

    CheckBoxState.UncheckedNormal;
                if (paintParts == DataGridViewPaintParts.Background || paintParts ==

    DataGridViewPaintParts.All)
                {
                    graphics.FillRectangle(new SolidBrush(cellStyle.BackColor), cellBounds);
                }
                if (paintParts == DataGridViewPaintParts.Border || paintParts ==

    DataGridViewPaintParts.All)
                {
                    graphics.DrawRectangle(new Pen(CellBorderColor), cellBounds);
                }
                if (paintParts == DataGridViewPaintParts.SelectionBackground || Selected)
                {
                    graphics.FillRectangle(new SolidBrush(cellStyle.SelectionBackColor),

    cellBounds);
                }
                var col = OwningColumn as DataGridViewCheckBoxTextColumn;
                if (col != null && !string.IsNullOrEmpty(col.Text))
                {
                    graphics.DrawString(col.Text, cellStyle.Font, new SolidBrush(Selected ?
                        cellStyle.SelectionForeColor : cellStyle.ForeColor),
                        new Point(cellBounds.X + 25, cellBounds.Y + cellBounds.Height / 4));
                }
                if (!string.IsNullOrEmpty(Text))
                {
                    graphics.DrawString(Text, cellStyle.Font, new SolidBrush(Selected ?
                      cellStyle.SelectionForeColor : cellStyle.ForeColor),
                      new Point(cellBounds.X + 25, cellBounds.Y + cellBounds.Height / 4));
                }
                CheckBoxRenderer.DrawCheckBox(graphics, new Point(cellBounds.X + 4, cellBounds.Y +

    cellBounds.Height / 4), CheckBoxTestState);
               // ButtonRenderer.DrawButton(graphics, new Rectangle(new Point(cellBounds.X + 50,

    cellBounds.Y + cellBounds.Height / 4), new Size(20, 20)), true, PushButtonState.Default);
               // base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,

    formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            }

            protected override void OnMouseDown(DataGridViewCellMouseEventArgs e)
            {
                var check = (bool)Value;
                CheckBoxTestState = check ? CheckBoxState.CheckedNormal :

    CheckBoxState.UncheckedNormal;
                Value = !check;
                base.OnMouseDown(e);
            }

            public string Text { get; set; }

        }
    }



    使用也很简单:(使用时先把结构加载完了在对这列进行如下操作)
      DataGridViewCheckBoxTestCell check3 = new DataGridViewCheckBoxTestCell();
                check3.Text = "请选择";
                check3.Value = true;
                row.Cells["check"] = check3;

  • 相关阅读:
    剑指offer题目整理
    字符串编辑距离
    团体程序设计天梯赛 L3-010 是否完全二叉搜索树 (30分)
    团体程序设计天梯赛 L3-006 迎风一刀斩 (30分)(几何规律题)
    团体程序设计天梯赛 L3-005 垃圾箱分布 (30分)(Djikstra求最短路)
    团体程序设计天梯赛 L3-004 肿瘤诊断 (30分)(DFS)
    团体程序设计天梯赛 L3-003 社交集群 (30分)(并查集)
    团体程序设计天梯赛 L3-002 特殊堆栈 (30分)(树状数组+二分)
    团体程序设计天梯赛 L3-001 凑零钱 (30分)(0/1背包)
    团体程序设计天梯赛 L2-012 关于堆的判断 (25分)
  • 原文地址:https://www.cnblogs.com/libinsoft/p/4201699.html
Copyright © 2020-2023  润新知