DataGridView の CheckBox に Text も表示する。

DataGridView ではチェックボックスだけが表示されテキストを表示できない。そこでチェックボックスに加えて、テキストも表示できるようにしてみた。

f:id:tt195361:20150707173705p:plain

DataGridViewCheckBoxCell クラスを継承し、テキストも表示できる CheckBoxAndTextCell クラスを作成する。

  • Text プロパティを追加。
  • Paint() で、base.Paint() を呼びチェックボックスを描画したあと、テキストを描画する。
  • GetPreferredSize() で、テキストを含めたサイズを返す。この値は自動サイズ設定で使われる。
  • Clone() で、追加した Text プロパティの値もコピーする。
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewCheckBoxAndTextCell
{
    public class CheckBoxAndTextCell : DataGridViewCheckBoxCell
    {
        private const Int32 PadLeft = 2;
        private const Int32 PadRight = 3;
        private const Int32 PadTop = 4;
        private const Int32 PadBottom = 3;

        private readonly TextFormatFlags FormatFlags = TextFormatFlags.Left | TextFormatFlags.EndEllipsis;

        private String _text = String.Empty;

        // チェックボックスの横に表示するテキストを取得、設定する。
        public String Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public override object DefaultNewRowValue
        {
            get { return false; }
        }

        protected override void Paint(Graphics graphics,
                                      Rectangle clipBounds, Rectangle cellBounds,
                                      Int32 rowIndex, DataGridViewElementStates cellState,
                                      Object value, Object formattedValue, String errorText,
                                      DataGridViewCellStyle cellStyle,
                                      DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                      DataGridViewPaintParts paintParts)
        {
            // DataGridViewCheckBoxCell.Paint で、チェックボックスを描画する。
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                       value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            // チェックボックスの横のセル内の残りスペースに、テキストを描画する。
            Rectangle checkBoxBounds = base.GetContentBounds(graphics, cellStyle, rowIndex);
            Point textLocation = GetTextLocation(cellBounds, checkBoxBounds);
            var availableTextSize = GetAvailableTextSize(cellBounds, checkBoxBounds);
            var availableTextRect = new Rectangle(textLocation, availableTextSize);
            TextRenderer.DrawText(graphics, Text,
                                  cellStyle.Font, availableTextRect, cellStyle.ForeColor, FormatFlags);
        }

        private Point GetTextLocation(Rectangle cellBounds, Rectangle contentBounds)
        {
            Int32 textX = cellBounds.X + contentBounds.Right + PadLeft;
            Int32 textY = cellBounds.Y + PadTop;
            var textLocation = new Point(textX, textY);
            return textLocation;
        }

        private Size GetAvailableTextSize(Rectangle cellBounds, Rectangle contentBounds)
        {
            Int32 textWidth = Math.Max(0, cellBounds.Width - contentBounds.Width - PadLeft - PadRight);
            Int32 textHeight = Math.Max(0, cellBounds.Height - PadBottom);
            var textSize = new Size(textWidth, textHeight);
            return textSize;
        }

        // カラムの自動サイズ設定に使用する。
        protected override Size GetPreferredSize(Graphics graphics,
                                                 DataGridViewCellStyle cellStyle, Int32 rowIndex,
                                                 Size constraintSize)
        {
            Rectangle checkBoxBounds = base.GetContentBounds(graphics, cellStyle, rowIndex);
            Size preferredTextSize = TextRenderer.MeasureText(graphics, Text, cellStyle.Font);
            Int32 contentWidth = checkBoxBounds.Width + preferredTextSize.Width + PadLeft + PadRight;
            Int32 contentHeight = Math.Max(checkBoxBounds.Height, preferredTextSize.Height + PadTop + PadBottom);
            var contentSize = new Size(contentWidth, contentHeight);
            return contentSize;
        }

        // 追加した Text プロパティもクローンに含める。
        public override object Clone()
        {
            var cloneCell = (CheckBoxAndTextCell)base.Clone();
            cloneCell.Text = this.Text;
            return cloneCell;
        }
    }
}

作成した CheckBoxAndTextCell クラスを DataGridView で使うには、

  • 追加するカラムの CellTemplate プロパティに CheckBoxAndTextCell のオブジェクトを設定する。
using System;
using System.Windows.Forms;

namespace DataGridViewCheckBoxAndTextCell
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AddColumn("カラム 1");

            AddRow("データ行 1");
            AddRow("データ行 2");
            AddRow("データ行 3");
        }

        private void AddColumn(String headerText)
        {
            var viewColumn = new DataGridViewColumn();
            viewColumn.HeaderText = headerText;
            // このカラムのセルは CheckBoxAndTextCell を使う。
            viewColumn.CellTemplate = new CheckBoxAndTextCell();
            dataGridView1.Columns.Add(viewColumn);
        }

        private void AddRow(String text)
        {
            Int32 rowIndex = dataGridView1.Rows.Add();
            DataGridViewRow viewRow = dataGridView1.Rows[rowIndex];
            CheckBoxAndTextCell chkAndTxtCell = (CheckBoxAndTextCell)viewRow.Cells[0];
            chkAndTxtCell.Value = false;
            chkAndTxtCell.Text = text;
        }
    }
}

参考: DataGridViewCheckBoxColumn with Text Information