Nasıl yapılır: Windows Forms DataGridView Denetiminde Davranış ve Görünümünü Genişleterek Hücre ve Sütunları Özelleştirme
Denetim DataGridView özellikleri, olayları ve yardımcı sınıfları kullanarak görünümünü ve davranışını özelleştirmek için çeşitli yollar sağlar. Bazen, hücreleriniz için bu özelliklerin sağ ara sıra sağlarını aşan gereksinimleriniz olabilir. Genişletilmiş işlevsellik sağlamak için DataGridViewCell kendi özel sınıfınızı oluşturabilirsiniz.
Temel sınıftan DataGridViewCell veya türetilmiş sınıflarından birini DataGridViewCell türeterek özel bir sınıf oluşturabilirsiniz. Herhangi bir sütun türünde herhangi bir hücre türünü görüntüleyebilirsiniz, ancak genellikle hücre türlerinizi görüntülemek için özelleştirilmiş bir DataGridViewColumn özel sınıf da oluşturabilirsiniz. Sütun sınıfları türetilen DataGridViewColumn türlerinden veya türetilen türlerinden biri.
Aşağıdaki kod örneğinde, farenin hücre sınırlarına ne zaman girdiği ve ayrıldığını algılayan adlı özel DataGridViewRolloverCell bir hücre sınıfı oluşturabilirsiniz. Fare hücrenin sınırları içindeyken bir küme dikdörtgeni çizilir. Bu yeni tür, 'den DataGridViewTextBoxCell türetulur ve diğer tüm bakımlardan temel sınıfı olarak davranır. Yardımcı sütun sınıfı olarak DataGridViewRolloverColumn çağrılır.
Bu sınıfları kullanmak için denetim içeren bir form oluşturun, koleksiyona bir veya daha fazla nesne ekleyin ve denetimi değer DataGridViewDataGridViewRolloverColumn içeren Columns satırlarla doldurun.
Not
Boş satırlar eklersanız bu örnek düzgün çalışmaz. Örneğin, özelliğini ayarerek denetime satırlar eklerken boş satırlar RowCount oluşturulur. Bunun nedeni, bu durumda eklenen satırların otomatik olarak paylaşılmış olmasıdır. Bu, tek tek hücrelere tıklanana kadar nesnelerin örneği oluşturmaz ve bu da ilişkili satırların paylaşılmasına neden DataGridViewRolloverCell olur.
Bu tür bir hücre özelleştirmesi paylaşılmayan satırlar gerektirdiği için büyük veri kümeleriyle kullanmak uygun değildir. Satır paylaşımı hakkında daha fazla bilgi için bkz. Windows Forms DataGridView Denetimini Ölçeklendirmeye Yönelik En İyi Yöntemler.
Not
veya türetilen sınıfından türetilen ve türetilmiş sınıfa yeni özellikler eklerken, kopyalama işlemleri sırasında yeni özellikleri kopyalamak için yöntemini DataGridViewCellDataGridViewColumn geçersiz Clone kılarak emin olun. Temel sınıfın özelliklerinin yeni hücreye veya sütuna kopyalandır olması için temel sınıfın Clone yöntemini de çağırabilirsiniz.
DataGridView denetiminde hücreleri ve sütunları özelleştirmek için
türünden adlı yeni bir hücre
DataGridViewRolloverCellsınıfı DataGridViewTextBoxCell türetin.public class DataGridViewRolloverCell : DataGridViewTextBoxCell {Public Class DataGridViewRolloverCell Inherits DataGridViewTextBoxCell}End Classsınıfındaki Paint yöntemini geçersiz
DataGridViewRolloverCellkılın. Geçersiz kılmada, önce barındırılan metin kutusu işlevselliğini ele alan temel sınıf uygulamasını çağırabilirsiniz. Ardından denetimin yöntemini PointToClient kullanarak imleç konumunu (ekran koordinatlarında) DataGridView istemci alanı koordinatlarına dönüştürebilirsiniz. Fare koordinatları hücrenin sınırları içinde yer alacaksa, küme dikdörtgenini çizin.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 SubFare işaretçisi onları girerken veya bırakırken hücreleri kendilerini yeniden boyatacak şekilde zorlamak için OnMouseEnter sınıfındaki ve OnMouseLeave yöntemlerini geçersiz
DataGridViewRolloverCellkılın.// 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 Subtüründen adlı yeni bir
DataGridViewRolloverCellColumnsınıf DataGridViewColumn türetin. Oluşturucuda, özelliğine yeniDataGridViewRolloverCellbir nesne CellTemplate attayabilirsiniz.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
Örnek
Tam kod örneği, özel hücre türünün davranışını gösteren küçük bir test formu içerir.
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
Kod Derleniyor
Bu örnek şunları gerektirir:
- Sistem, Sistem başvuruları. Windows. Forms ve System.Drawing derlemeleri.