方法: Windows フォーム DataGridView コントロールのセルと列を、それぞれの動作と外観を拡張してカスタマイズするHow to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance

DataGridView コントロールは、プロパティ、イベント、およびコンパニオン クラスを使用して外観と動作をカスタマイズする様々な方法を提供します。The DataGridView control provides a number of ways to customize its appearance and behavior using properties, events, and companion classes. 場合によっては、これらの機能が提供するもの以外にも、セルの要件がある場合があります。Occasionally, you may have requirements for your cells that go beyond what these features can provide. 独自のカスタム DataGridViewCell クラスを作成して、拡張機能を提供することができます。You can create your own custom DataGridViewCell class to provide extended functionality.

DataGridViewCell 基底クラスまたは派生クラスの 1 つから派生することで、カスタム DataGridViewCell クラスを作成します。You create a custom DataGridViewCell class by deriving from the DataGridViewCell base class or one of its derived classes. 任意の種類の列の任意の種類のセルを表示できますが、多くの場合は、セルの種類の表示に特化したカスタムの DataGridViewColumn クラスを作成することになります。Although you can display any type of cell in any type of column, you will typically also create a custom DataGridViewColumn class specialized for displaying your cell type. 列のクラスは、DataGridViewColumn または派生型のいずれかから派生します。Column classes derive from DataGridViewColumn or one of its derived types.

次のコード例では、マウスがセルの境界に入ってでるときを検出する、DataGridViewRolloverCell と呼ばれるカスタム セル クラスを作成します。In the following code example, you will create a custom cell class called DataGridViewRolloverCell that detects when the mouse enters and leaves the cell boundaries. マウスがセルの範囲内にある場合に、埋め込みの四角形が描画されます。While the mouse is within the cell's bounds, an inset rectangle is drawn. この新しい型は DataGridViewTextBoxCell から派生して、その他のすべての点では、基底クラスとして動作します。This new type derives from DataGridViewTextBoxCell and behaves in all other respects as its base class. 列のコンパニオン クラスは DataGridViewRolloverColumn と呼ばれます。The companion column class is called DataGridViewRolloverColumn.

これらのクラスを使用するには、DataGridView コントロールを含むフォームを作成して、1 つ以上の DataGridViewRolloverColumn オブジェクトを Columns コレクションに追加し、値を含む行にコントロールを設定します。To use these classes, create a form containing a DataGridView control, add one or more DataGridViewRolloverColumn objects to the Columns collection, and populate the control with rows containing values.

注意

空の行を追加すると、この例は正しく動作しません。This example will not work correctly if you add empty rows. たとえば、RowCount プロパティを設定することでコントロールに行を追加する場合に、空の行を作成します。Empty rows are created, for example, when you add rows to the control by setting the RowCount property. これは、この例で追加された行は自動的に共有されるためでです。つまり、DataGridViewRolloverCell オブジェクトは、各セルをクリックするまでインスタンス化されないため、関連付けられた行の共有が解除されます。This is because the rows added in this case are automatically shared, which means that DataGridViewRolloverCell objects are not instantiated until you click on individual cells, thereby causing the associated rows to become unshared.

この種類のセルのカスタマイズには共有されていない行が必要なため、大量のデータ セットでの使用には適切ではありません。Because this type of cell customization requires unshared rows, it is not appropriate for use with large data sets. 行の共有の詳細については、「 Windows フォーム DataGridView コントロールのスケーリングに関するベストプラクティス」を参照してください。For more information about row sharing, see Best Practices for Scaling the Windows Forms DataGridView Control.

注意

DataGridViewCellDataGridViewColumn から派生したクラスに新しいプロパティを追加するときは、Clone メソッドをオーバーライドし、複製操作時に新しいプロパティをコピーする必要があります。When you derive from DataGridViewCell or DataGridViewColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. また、基底クラスの Clone メソッドを呼び出して、基底クラスのプロパティを新しいセルまたは列にコピーする必要もあります。You should also call the base class's Clone method so that the properties of the base class are copied to the new cell or column.

DataGridView コントロール内でセルと列をカスタマイズするにはTo customize cells and columns in the DataGridView control

  1. DataGridViewRolloverCellという新しいセル クラスを DataGridViewTextBoxCell 型から派生させます。Derive a new cell class, called DataGridViewRolloverCell, from the DataGridViewTextBoxCell type.

    public class DataGridViewRolloverCell : DataGridViewTextBoxCell
    {
    
    Public Class DataGridViewRolloverCell
        Inherits DataGridViewTextBoxCell
    
    }
    
    End Class
    
  2. Paint クラスの DataGridViewRolloverCell メソッドをオーバーライドします。Override the Paint method in the DataGridViewRolloverCell class. オーバーライドで最初にホストされているテキスト ボックスの機能を処理する基本クラスの実装を呼び出します。In the override, first call the base class implementation, which handles the hosted text box functionality. 次に、コントロールの PointToClient メソッドを使用して、(画面座標の) カーソル位置を DataGridView クライアント領域の座標に変換します。Then use the control's PointToClient method to transform the cursor position (in screen coordinates) to the DataGridView client area's coordinates. マウスの座標がセルの境界内にある場合は、埋め込みの四角形を描画します。If the mouse coordinates fall within the bounds of the cell, draw the inset rectangle.

    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)
    {
        // Call the base class method to paint the default cell appearance.
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
            value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);
    
        // Retrieve the client location of the mouse pointer.
        Point cursorPosition =
            this.DataGridView.PointToClient(Cursor.Position);
    
        // If the mouse pointer is over the current cell, draw a custom border.
        if (cellBounds.Contains(cursorPosition))
        {
            Rectangle newRect = new Rectangle(cellBounds.X + 1,
                cellBounds.Y + 1, cellBounds.Width - 4,
                cellBounds.Height - 4);
            graphics.DrawRectangle(Pens.Red, newRect);
        }
    }
    
    Protected Overrides Sub Paint( _
        ByVal graphics As Graphics, _
        ByVal clipBounds As Rectangle, _
        ByVal cellBounds As Rectangle, _
        ByVal rowIndex As Integer, _
        ByVal elementState As DataGridViewElementStates, _
        ByVal value As Object, _
        ByVal formattedValue As Object, _
        ByVal errorText As String, _
        ByVal cellStyle As DataGridViewCellStyle, _
        ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
        ByVal paintParts As DataGridViewPaintParts)
    
        ' Call the base class method to paint the default cell appearance.
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, _
            value, formattedValue, errorText, cellStyle, _
            advancedBorderStyle, paintParts)
    
        ' Retrieve the client location of the mouse pointer.
        Dim cursorPosition As Point = _
            Me.DataGridView.PointToClient(Cursor.Position)
    
        ' If the mouse pointer is over the current cell, draw a custom border.
        If cellBounds.Contains(cursorPosition) Then
            Dim newRect As New Rectangle(cellBounds.X + 1, _
                cellBounds.Y + 1, cellBounds.Width - 4, _
                cellBounds.Height - 4)
            graphics.DrawRectangle(Pens.Red, newRect)
        End If
    
    End Sub
    
  3. DataGridViewRolloverCell クラスの OnMouseEnter メソッドと OnMouseLeave メソッドをオーバーライドして、マウス ポインターがに入る時点または出る時点でセルが自身を再描画するよう強制します。Override the OnMouseEnter and OnMouseLeave methods in the DataGridViewRolloverCell class to force cells to repaint themselves when the mouse pointer enters or leaves them.

    // Force the cell to repaint itself when the mouse pointer enters it.
    protected override void OnMouseEnter(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }
    
    // Force the cell to repaint itself when the mouse pointer leaves it.
    protected override void OnMouseLeave(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }
    
    ' Force the cell to repaint itself when the mouse pointer enters it.
    Protected Overrides Sub OnMouseEnter(ByVal rowIndex As Integer)
        Me.DataGridView.InvalidateCell(Me)
    End Sub
    
    ' Force the cell to repaint itself when the mouse pointer leaves it.
    Protected Overrides Sub OnMouseLeave(ByVal rowIndex As Integer)
        Me.DataGridView.InvalidateCell(Me)
    End Sub
    
  4. DataGridViewRolloverCellColumn という新しいクラスを DataGridViewColumn 型から派生させます。Derive a new class, called DataGridViewRolloverCellColumn, from the DataGridViewColumn type. コンストラクターで、新しい DataGridViewRolloverCell オブジェクトを CellTemplate プロパティに割り当てます。In the constructor, assign a new DataGridViewRolloverCell object to its CellTemplate property.

    public class DataGridViewRolloverCellColumn : DataGridViewColumn
    {
        public DataGridViewRolloverCellColumn()
        {
            this.CellTemplate = new DataGridViewRolloverCell();
        }
    }
    
    Public Class DataGridViewRolloverCellColumn
        Inherits DataGridViewColumn
    
        Public Sub New()
            Me.CellTemplate = New DataGridViewRolloverCell()
        End Sub
    
    End Class
    

Example

完全なコード例には、カスタムのセルの種類の動作を示す小さなテスト フォームが含まれます。The complete code example includes a small test form that demonstrates the behavior of the custom cell type.

using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        DataGridView dataGridView1 = new DataGridView();
        DataGridViewRolloverCellColumn col =
            new DataGridViewRolloverCellColumn();
        dataGridView1.Columns.Add(col);
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        this.Controls.Add(dataGridView1);
        this.Text = "DataGridView rollover-cell demo";
    }
}

public class DataGridViewRolloverCell : DataGridViewTextBoxCell
{
    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)
    {
        // Call the base class method to paint the default cell appearance.
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
            value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);

        // Retrieve the client location of the mouse pointer.
        Point cursorPosition =
            this.DataGridView.PointToClient(Cursor.Position);

        // If the mouse pointer is over the current cell, draw a custom border.
        if (cellBounds.Contains(cursorPosition))
        {
            Rectangle newRect = new Rectangle(cellBounds.X + 1,
                cellBounds.Y + 1, cellBounds.Width - 4,
                cellBounds.Height - 4);
            graphics.DrawRectangle(Pens.Red, newRect);
        }
    }

    // Force the cell to repaint itself when the mouse pointer enters it.
    protected override void OnMouseEnter(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }

    // Force the cell to repaint itself when the mouse pointer leaves it.
    protected override void OnMouseLeave(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }

}

public class DataGridViewRolloverCellColumn : DataGridViewColumn
{
    public DataGridViewRolloverCellColumn()
    {
        this.CellTemplate = new DataGridViewRolloverCell();
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Class Form1
    Inherits Form

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Public Sub New()
        Dim dataGridView1 As New DataGridView()
        Dim col As New DataGridViewRolloverCellColumn()
        dataGridView1.Columns.Add(col)
        dataGridView1.Rows.Add(New String() {""})
        dataGridView1.Rows.Add(New String() {""})
        dataGridView1.Rows.Add(New String() {""})
        dataGridView1.Rows.Add(New String() {""})
        Me.Controls.Add(dataGridView1)
        Me.Text = "DataGridView rollover-cell demo"
    End Sub

End Class

Public Class DataGridViewRolloverCell
    Inherits DataGridViewTextBoxCell

    Protected Overrides Sub Paint( _
        ByVal graphics As Graphics, _
        ByVal clipBounds As Rectangle, _
        ByVal cellBounds As Rectangle, _
        ByVal rowIndex As Integer, _
        ByVal elementState As DataGridViewElementStates, _
        ByVal value As Object, _
        ByVal formattedValue As Object, _
        ByVal errorText As String, _
        ByVal cellStyle As DataGridViewCellStyle, _
        ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
        ByVal paintParts As DataGridViewPaintParts)

        ' Call the base class method to paint the default cell appearance.
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, _
            value, formattedValue, errorText, cellStyle, _
            advancedBorderStyle, paintParts)

        ' Retrieve the client location of the mouse pointer.
        Dim cursorPosition As Point = _
            Me.DataGridView.PointToClient(Cursor.Position)

        ' If the mouse pointer is over the current cell, draw a custom border.
        If cellBounds.Contains(cursorPosition) Then
            Dim newRect As New Rectangle(cellBounds.X + 1, _
                cellBounds.Y + 1, cellBounds.Width - 4, _
                cellBounds.Height - 4)
            graphics.DrawRectangle(Pens.Red, newRect)
        End If

    End Sub

    ' Force the cell to repaint itself when the mouse pointer enters it.
    Protected Overrides Sub OnMouseEnter(ByVal rowIndex As Integer)
        Me.DataGridView.InvalidateCell(Me)
    End Sub

    ' Force the cell to repaint itself when the mouse pointer leaves it.
    Protected Overrides Sub OnMouseLeave(ByVal rowIndex As Integer)
        Me.DataGridView.InvalidateCell(Me)
    End Sub

End Class

Public Class DataGridViewRolloverCellColumn
    Inherits DataGridViewColumn

    Public Sub New()
        Me.CellTemplate = New DataGridViewRolloverCell()
    End Sub

End Class

コードのコンパイルCompiling the Code

この例で必要な要素は次のとおりです。This example requires:

  • System、System.Windows.Forms、および System.Drawing の各アセンブリへの参照。References to the System, System.Windows.Forms, and System.Drawing assemblies.

関連項目See also