星期六, 8月 03, 2024

[C#] 自訂儲存格外觀

根據官方文件 - 如何:在 Windows Form DataGridView 控制項中自訂儲存格的外觀 來練習,除了文章內容外,另外也新增自訂欄位來驗證 GridLine


C# Code

繪製步驟依循下述步驟
  • 繪製背景顏色
  • 繪製 GridLine
  • 繪製自訂框線
  • 繪製文字內容
兩欄位使用函數差異在於
  • 繪製 GridLine:官方 DrawLine()、自訂 PaintBackground()
  • 繪製文字內容:官方 DrawString()、自訂 PaintContent()
namespace CellPaintingEvent
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            dataGridView1.RowCount = 5;
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[i].Cells[ColContactName.Index].Value = $"Contact-{i}";
                dataGridView1.Rows[i].Cells[ColCustom.Index].Value = $"自訂內容-{i}";
            }
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex < 0)
                return;

            using Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);

            if (dataGridView1.Columns[ColContactName.Index].Index == e.ColumnIndex)
            {
                using Pen gridLinePen = new Pen(dataGridView1.GridColor);

                // Erase the cell.
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Draw the grid lines(only the right and bottom lines;
                // DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen,
                    e.CellBounds.Left,
                    e.CellBounds.Bottom - 1,
                    e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);

                e.Graphics.DrawLine(gridLinePen,
                    e.CellBounds.Right - 1,
                    e.CellBounds.Top,
                    e.CellBounds.Right - 1,
                    e.CellBounds.Bottom);

                // Draw the inset highlight box.
                Rectangle newRect = new Rectangle(
                    e.CellBounds.X + 1,
                    e.CellBounds.Y + 1,
                    e.CellBounds.Width - 4,
                    e.CellBounds.Height - 4);

                e.Graphics.DrawRectangle(Pens.Blue, newRect);

                // Draw the text content of the cell, ignoring alignment.
                if (e.Value != null)
                {
                    e.Graphics.DrawString(
                        (string)e.Value,
                        e.CellStyle.Font,
                        Brushes.Crimson,
                        e.CellBounds.X + 2,
                        e.CellBounds.Y + 2,
                        StringFormat.GenericDefault);
                }

                e.Handled = true;
                return;
            }


            if (dataGridView1.Columns[ColCustom.Index].Index == e.ColumnIndex)
            {
                // Step1:繪製背景顏色
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Step2:繪製 GridLine
                e.PaintBackground(e.ClipBounds, true);

                // Step3:繪製自訂框線
                if (e.RowIndex % 2 == 0)
                {
                    using Pen alarmPen = new Pen(Color.LightCoral, 3);

                    Rectangle rect = new Rectangle(
                        e.CellBounds.X,
                        e.CellBounds.Y,
                        e.CellBounds.Width - 2,
                        e.CellBounds.Height - 2);

                    e.Graphics.DrawRectangle(alarmPen, rect);
                }

                // Step4:繪製文字內容
                if (e.Value != null)
                    e.PaintContent(e.ClipBounds);

                e.Handled = true;
                return;
            }
        }
    }
}

GridLine

文件上特別提到 GridLine 只要繪製右方和下方線條就行,自訂外框欄位上特別用內建函數 PaintBackground() 來驗證看看。下述驗證語法為只在偶數列執行 PaintBackground() 來進行繪製,PaintBackground() 不會繪製背景,只有 GridLine 而已喔
if (dataGridView1.Columns[ColCustom.Index].Index == e.ColumnIndex)
{
    if (e.RowIndex % 2 != 0)
        e.PaintBackground(e.ClipBounds, true);

    e.Handled = true;
    return;
}
下圖官方欄位不繪製 GridLine,而自訂欄位只繪製偶數列 GridLine

沒有留言:

張貼留言