Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
A DataGridView vezérlő számos módszert kínál a megjelenésének és viselkedésének testreszabására tulajdonságok, események és társosztályok használatával. Időnként előfordulhat, hogy a cellákra vonatkozó követelmények túllépnek azon, amit ezek a funkciók nyújthatnak. Létrehozhatja saját egyéni DataGridViewCell osztályát a kiterjesztett funkciók biztosításához.
Egyéni DataGridViewCell osztályt úgy hozhat létre, hogy az DataGridViewCell alaposztályból vagy annak egyik származtatott osztályából származik. Bár bármilyen típusú cellát megjeleníthet bármilyen típusú oszlopban, általában létrehoz egy egyéni DataGridViewColumn osztályt is, amely a cellatípus megjelenítésére specializálódott. Az oszloposztályok DataGridViewColumn vagy annak egyik származtatott típusából származnak.
A következő kód példában egy DataGridViewRolloverCell nevű egyéni cellaosztályt fog létrehozni, amely észleli, hogy az egér mikor lép be és hagyja el a cellahatárokat. Amíg az egér a cella határain belül van, egy bevágott téglalap rajzolódik. Ez az új típus a DataGridViewTextBoxCell származik, és minden más szempontból alaposztályként viselkedik. A társoszloposztály neve DataGridViewRolloverColumn.
Az osztályok használatához hozzon létre egy DataGridView vezérlőelemet tartalmazó űrlapot, vegyen fel egy vagy több DataGridViewRolloverColumn objektumot a Columns gyűjteménybe, és töltse fel a vezérlőt értékeket tartalmazó sorokkal.
Megjegyzés:
Ez a példa nem fog megfelelően működni, ha üres sorokat ad hozzá. Az üres sorok például akkor jönnek létre, amikor sorokat ad hozzá a vezérlőelemhez a RowCount tulajdonság beállításával. Ennek az az oka, hogy az ebben az esetben hozzáadott sorok automatikusan meg vannak osztva, ami azt jelenti, hogy DataGridViewRolloverCell objektumok nem lesznek példányosítva, amíg az egyes cellákra nem kattint, így a társított sorok nem lesznek szétosztva.
Mivel az ilyen típusú cella testreszabása nem tagolt sorokat igényel, nagy adatkészletek esetén nem megfelelő. További információ a sormegosztásról: Ajánlott eljárások a Windows Forms DataGridView vezérlőskálázására.
Megjegyzés:
Ha a DataGridViewCell-ból vagy a DataGridViewColumn-ből származtatja az osztályt, és új tulajdonságokat ad hozzá, feltétlenül felül kell bírálni a Clone módszert, hogy az új tulajdonságok klónozási műveletek során másolásra kerüljenek. Az alaposztály Clone metódusát is meg kell hívnia, hogy az alaposztály tulajdonságai át legyenek másolva az új cellába vagy oszlopba.
Cellák és oszlopok testreszabása a DataGridView vezérlőben
A
DataGridViewRolloverCelltípusból származtat egy új, DataGridViewTextBoxCellnevű cellaosztályt.public class DataGridViewRolloverCell : DataGridViewTextBoxCell {Public Class DataGridViewRolloverCell Inherits DataGridViewTextBoxCell}End ClassFelülbírálja a Paint metódust a
DataGridViewRolloverCellosztályban. A felülbírálásnál először hívja meg az alaposztály implementációját, amely kezeli az üzemeltetett szövegdoboz funkcióit. Ezután a vezérlő PointToClient metódusával alakítsa át a kurzor pozícióját (a képernyő koordinátáiban) a DataGridView ügyfélterület koordinátáira. Ha az egér koordinátái a cella határán belülre esnek, rajzolja meg az inset téglalapot.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 SubA OnMouseEnter osztály OnMouseLeave és
DataGridViewRolloverCellmetódusainak felülbírálása biztosítja, hogy a cellák újrafestődjenek, amikor az egérmutató belép vagy elhagyja őket.// 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 SubA
DataGridViewRolloverCellColumntípusból származtat egy új, DataGridViewColumnnevű osztályt. A konstruktorban rendeljen hozzá egy újDataGridViewRolloverCellobjektumot a CellTemplate tulajdonságához.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
példa
A teljes példakód egy kis tesztűrlapot tartalmaz, amely bemutatja az egyéni cellatípus viselkedését.
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
A kód összeállítása
Ehhez a példához a következőre van szükség:
- A System, a System.Windows.Forms és a System.Drawing összetevőkre mutató hivatkozások.
Lásd még
.NET Desktop feedback