Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Das DataGridView Steuerelement bietet eine Reihe von Möglichkeiten zum Anpassen der Darstellung und des Verhaltens mithilfe von Eigenschaften, Ereignissen und Begleitklassen. Gelegentlich haben Sie möglicherweise Anforderungen für Ihre Zellen, die über die Funktionen hinausgehen. Sie können eine eigene benutzerdefinierte DataGridViewCell Klasse erstellen, um erweiterte Funktionen bereitzustellen.
Sie erstellen eine benutzerdefinierte DataGridViewCell Klasse, indem Sie von der DataGridViewCell Basisklasse oder einer ihrer abgeleiteten Klassen ableiten. Sie können zwar jeden Zelltyp in einem beliebigen Spaltentyp anzeigen, sie erstellen jedoch in der Regel auch eine benutzerdefinierte DataGridViewColumn Klasse, die speziell für die Anzeige Des Zelltyps spezialisiert ist. Spaltenklassen werden von DataGridViewColumn oder von einem der abgeleiteten Typen abgeleitet.
Im folgenden Codebeispiel erstellen Sie eine benutzerdefinierte Zellenklasse, die erkennt, wann die Maus die Zellgrenzen betritt und verlässt. Während sich die Maus innerhalb der Grenzen der Zelle befindet, wird ein eingebettetes Rechteck gezeichnet. Dieser neue Typ leitet sich von DataGridViewTextBoxCell ab und verhält sich in allen anderen Hinsichten wie seine Basisklasse. Die Begleitspaltenklasse heißt DataGridViewRolloverColumn
.
Um diese Klassen zu verwenden, erstellen Sie ein Formular, das ein DataGridView Steuerelement enthält, fügen Sie der DataGridViewRolloverColumn
Auflistung ein oder Columns mehrere Objekte hinzu, und füllen Sie das Steuerelement mit Zeilen mit Werten auf.
Hinweis
Dieses Beispiel funktioniert nicht ordnungsgemäß, wenn Sie leere Zeilen hinzufügen. Leere Zeilen werden beispielsweise erstellt, wenn Sie dem Steuerelement Zeilen hinzufügen, indem Sie die RowCount Eigenschaft festlegen. Dies liegt daran, dass die in diesem Fall hinzugefügten Zeilen automatisch freigegeben werden, was bedeutet, dass DataGridViewRolloverCell
Objekte erst instanziiert werden, wenn Sie auf einzelne Zellen klicken, wodurch die zugeordneten Zeilen nicht mehr freigegeben werden.
Da für diese Art von Zellanpassung nicht geteilte Zeilen erforderlich sind, ist sie nicht für die Verwendung mit großen Datensätzen geeignet. Weitere Informationen zur Zeilenfreigabe finden Sie unter Best Practices for Scaling the Windows Forms DataGridView Control.
Hinweis
Wenn Sie aus DataGridViewCell oder DataGridViewColumn ableiten und der abgeleiteten Klasse neue Eigenschaften hinzufügen, müssen Sie die Clone
-Methode überschreiben, damit die neuen Eigenschaften bei Klonvorgängen kopiert werden. Sie sollten auch die Clone
-Methode der Basisklasse aufrufen, damit die Eigenschaften der Basisklasse in die neue Zelle oder Spalte kopiert werden.
So passen Sie Zellen und Spalten im DataGridView-Steuerelement an
Leiten Sie eine neue Zellenklasse namens
DataGridViewRolloverCell
ab, die vom Typ DataGridViewTextBoxCell ist.public class DataGridViewRolloverCell : DataGridViewTextBoxCell {
Public Class DataGridViewRolloverCell Inherits DataGridViewTextBoxCell
}
End Class
Überschreiben Sie die Paint Methode in der
DataGridViewRolloverCell
Klasse. Rufen Sie in der Außerkraftsetzung zuerst die Basisklassenimplementierung auf, die die gehostete Textfeldfunktion behandelt. Verwenden Sie dann die Methode des PointToClient Steuerelements, um die Cursorposition (in Bildschirmkoordinaten) in die Koordinaten des DataGridView Clientbereichs zu transformieren. Wenn die Mauskoordinaten innerhalb der Grenzen der Zelle liegen, zeichnen Sie das Einsetrechteck.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
Überschreiben Sie die OnMouseEnter- und OnMouseLeave-Methoden in der
DataGridViewRolloverCell
-Klasse, um zu erzwingen, dass die Zellen sich selbst neu zeichnen, wenn der Mauszeiger sie betritt oder verlässt.// 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
Leiten Sie eine neue Klasse namens
DataGridViewRolloverCellColumn
von dem Typ DataGridViewColumn ab. Weisen Sie im Konstruktor derDataGridViewRolloverCell
-Eigenschaft ein neues CellTemplate-Objekt zu.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
Beispiel
Das vollständige Codebeispiel enthält ein kleines Testformular, das das Verhalten des benutzerdefinierten Zelltyps veranschaulicht.
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
Code kompilieren
In diesem Beispiel ist Folgendes erforderlich:
- Verweise auf die Assemblys "System", "System.Windows.Forms" und "System.Drawing".
Siehe auch
.NET Desktop feedback