1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DataGridView でセル毎に色分けして表示

Last updated at Posted at 2025-09-24

CustomDataGridViewTextBoxColumn & CustomDataGridViewTextBoxCell

要点は CustomDataGridViewTextBoxCell の Paint() の オーバーライドです

sample1.jpg

CustomDataGridViewTextBoxColumn.cs
using System.Drawing.Drawing2D;

namespace System.Windows.Forms
{
    public class CustomDataGridViewTextBoxColumn : DataGridViewColumn
    {
        public CustomDataGridViewTextBoxColumn() : base(new CustomDataGridViewTextBoxCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (!(value is CustomDataGridViewTextBoxCell))
                {
                    throw new InvalidCastException("CustomDataGridViewTextBoxCellオブジェクトを指定してください。");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class CustomDataGridViewTextBoxCell : DataGridViewTextBoxCell
    {
        public static DateTime dt;

        static DateTime GetNowDate()
        {
            long ticks = DateTime.Now.Ticks;

            return new DateTime(ticks - (ticks % TimeSpan.TicksPerDay));
        }

        static CustomDataGridViewTextBoxCell()
        {
            dt = GetNowDate();
        }

        public static void UpdateNow()
        {
            dt = GetNowDate();
        }

        public CustomDataGridViewTextBoxCell()
        {
        }

        public static HatchBrush brush1 = new HatchBrush(HatchStyle.Percent50, Color.DarkGray, Color.White);

        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)
        {
            Date? date = value as Date;

            if (date != null)
            {
                long days = (date.dt.Ticks - CustomDataGridViewTextBoxCell.dt.Ticks) / TimeSpan.TicksPerDay;

                if (days < 0)
                {
                    if (days >= -30)
                    {
                        cellStyle.ForeColor = Color.Red;
                    }
                    else
                    {
                        cellStyle.BackColor = Color.LightPink;
                    }
                }
                else
                {
                    if (days <= 30)
                    {
                        cellStyle.ForeColor = Color.Blue;
                    }
                    else
                    {
                        cellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
            else
            {
                graphics.FillRectangle(brush1, cellBounds);

                paintParts &= ~DataGridViewPaintParts.Background;
            }

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }

    public class Date : IComparable<Date>
    {
        public DateTime dt;

        public string? text;

        public Date(DateTime dt)
        {
            this.dt = dt;
            this.text = null;
        }

        public Date(long ticks) : this(new DateTime(TrimDay(ticks))) { }

        public Date(int year, int month, int day)
        {
            Debug.Assert(year > 0 && month >= 1 && month <= 12 && day >= 1 && day <= 31);

            this.dt = new DateTime(year, month, day);

            this.text = null;
        }

        public Date(Date date) : this(date.dt, date.text) { }

        public Date(string date)
        {
            this.dt = DateTime.Parse(date);

            this.text = null;
        }

        protected Date(DateTime dt, string? text)
        {
            this.dt = dt;
            this.text = text;
        }

        public static long TrimDay(long tick)
        {
            return tick - (tick % TimeSpan.TicksPerDay);
        }

        static readonly string[] day_of_weeks = { "日", "月", "火", "水", "木", "金", "土" };

        protected string GetText()
        {
            return dt.ToString("yyyy/MM/dd") + "(" + day_of_weeks[(int)dt.DayOfWeek] + ")";
        }

        public void SetText()
        {
            text = GetText();
        }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(text))
            {
                text = GetText();
            }

            return text;
        }

        public int CompareTo(Date? other)
        {
            if (other == null)
            {
                return 1;
            }

            return Comparer<DateTime>.Default.Compare(dt, other.dt);
        }
    }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?