日付型セル(DateTimePickerCell)の値がnullのとき、代替文字列を表示するには?

文書番号 : 40556     文書種別 : 使用方法     登録日 : 2017/07/26     最終更新日 : 2017/07/26
文書を印刷する
対象製品
MultiRow for Windows Forms 10.0J
詳細
DateTimePickerCellの値がnull(Visual BasicではNothing)のときに代替文字列を表示するには、セルの継承またはオーナー描画を行う必要があります。

DateTimePickerCellを継承してユーザー定義型セルを作成する場合

[Visual Basic]
Imports System.Windows.Forms
Imports GrapeCity.Win.MultiRow

Public Class MyDateTimePickerCell
    Inherits DateTimePickerCell

    Protected Overrides Sub OnPaint(ByVal e As GrapeCity.Win.MultiRow.CellPaintingEventArgs)
        MyBase.OnPaint(e)
        If e.Value Is Nothing Then
            Dim placeHolder As String = "(なし)"
            e.PaintBackground(e.ClipBounds)
            Dim brushColor As Color = IIf(e.Selected, e.CellStyle.SelectionForeColor, e.CellStyle.ForeColor)
            If e.Enabled = False Then brushColor = e.CellStyle.DisabledForeColor
            Using brush As New SolidBrush(brushColor)
                Dim contentSize As SizeF = e.Graphics.MeasureString(placeHolder, e.CellStyle.Font)
                Dim y As Double = e.CellBounds.Y + ((e.CellBounds.Height - contentSize.Height) / 2)
                e.Graphics.DrawString(placeHolder, e.CellStyle.Font, brush, e.ClipBounds.X, y)
            End Using
        End If
    End Sub

End Class


[C#]
using System.Drawing;
using GrapeCity.Win.MultiRow;

public class MyDateTimePickerCell : DateTimePickerCell
{

    protected override void OnPaint(CellPaintingEventArgs e)
    {
        base.OnPaint(e);
        if (e.Value != null)
        {
            string placeHolder = "(なし)";
            Color brushColor = e.Selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor;
            if (e.Enabled == false)
                brushColor = e.CellStyle.DisabledForeColor;

            using (Brush brush = new SolidBrush(brushColor))
            {
                SizeF contentSize = e.Graphics.MeasureString(placeHolder, e.CellStyle.Font);
                float y = e.CellBounds.Y + ((e.CellBounds.Height - contentSize.ToSize().Height) / 2);
                e.Graphics.DrawString(placeHolder, e.CellStyle.Font, brush, e.CellBounds.X, y);
            }
        }
    }
}


GcMultiRow コントロールでオーナー描画する場合
[Visual Basic]
Imports GrapeCity.Win.MultiRow

Private Sub GcMultiRow1_CellPainting(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.CellPaintingEventArgs) Handles GcMultiRow1.CellPainting
    Dim gcMultiRow As GcMultiRow = TryCast(sender, GcMultiRow)
    If TypeOf gcMultiRow.Rows(e.RowIndex).Cells(e.CellIndex) Is DateTimePickerCell Then
        If e.Value Is Nothing Then
            Dim placeHolder As String = "(なし)"
            e.PaintBackground(e.ClipBounds)
            Dim brushColor As Color = IIf(e.Selected, e.CellStyle.SelectionForeColor, e.CellStyle.ForeColor)
            Using brush As New SolidBrush(brushColor)
                Dim contentSize As SizeF = e.Graphics.MeasureString(placeHolder, e.CellStyle.Font)
                Dim y As Double = e.CellBounds.Y + ((e.CellBounds.Height - contentSize.Height) / 2)
                e.Graphics.DrawString(placeHolder, e.CellStyle.Font, brush, e.ClipBounds.X, y)
            End Using
            e.PaintErrorIcon(e.ClipBounds)
            e.PaintWaveLine(e.ClipBounds)
            e.PaintBorder(e.ClipBounds)

            e.Handled = True
        End If
    End If
End Sub


[C#]
using GrapeCity.Win.MultiRow;

private void gcMultiRow1_CellPainting(object sender, GrapeCity.Win.MultiRow.CellPaintingEventArgs e)
{
    GcMultiRow gcMultiRow = sender as GcMultiRow;
    if (gcMultiRow.Rows[e.RowIndex].Cells[e.CellIndex] is DateTimePickerCell)
    {
        if (e.Value == null)
        {
            string placeHolder = "(なし)";
            e.PaintBackground(e.ClipBounds);
            Color brushColor = e.Selected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor;
            using (Brush brush = new SolidBrush(brushColor))
            {
                SizeF contentSize = e.Graphics.MeasureString(placeHolder, e.CellStyle.Font);
                float y = e.CellBounds.Y + ((e.CellBounds.Height - contentSize.ToSize().Height) / 2);
                e.Graphics.DrawString(placeHolder, e.CellStyle.Font, brush, e.CellBounds.X, y);
            }
            e.PaintErrorIcon(e.ClipBounds);
            e.PaintWaveLine(e.ClipBounds);
            e.PaintBorder(e.ClipBounds);

            e.Handled = true;
        }
    }
}


GcMultiRow コントロールでオーナー描画する場合はセルのドロップダウンボタンを予め描画することができないため、独自に描画処理を追加する必要があります。