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ğlayabileceklerini aşan gereksinimleriniz olabilir. Genişletilmiş işlevsellik sağlamak için kendi özel DataGridViewCell sınıfınızı oluşturabilirsiniz.
Temel sınıftan DataGridViewCell veya türetilmiş sınıflarından birinden türeterek özel DataGridViewCell bir sınıf oluşturursunuz. Her tür sütunda herhangi bir hücre türünü görüntüleyebilmenize rağmen, genellikle hücre türünüzü görüntülemek için özelleştirilmiş bir özel DataGridViewColumn sınıf da oluşturursunuz. Sütun sınıfları veya DataGridViewColumn türetilmiş türlerinden biri türetilir.
Aşağıdaki kod örneğinde, farenin hücre sınırlarını ne zaman girip ayrıldığını algılayan adlı DataGridViewRolloverCell
özel bir hücre sınıfı oluşturacaksınız. Fare hücrenin sınırları içindeyken, bir inset dikdörtgen çizilir. Bu yeni tür, öğesinden türetilir ve temel sınıfı olarak diğer tüm açılardan DataGridViewTextBoxCell davranır. Yardımcı sütun sınıfı olarak adlandırılır DataGridViewRolloverColumn
.
Bu sınıfları kullanmak için denetim içeren bir form oluşturun, koleksiyona Columns bir DataGridView veya daha fazla DataGridViewRolloverColumn
nesne ekleyin ve denetimi değer içeren satırlarla doldurun.
Dekont
Boş satırlar eklerseniz bu örnek düzgün çalışmaz. Örneğin, özelliğini ayarlayarak RowCount denetime satır eklediğinizde boş satırlar oluşturulur. Bunun nedeni, bu durumda eklenen satırların otomatik olarak paylaşılıyor olmasıdır; bu DataGridViewRolloverCell
da tek tek hücrelere tıklanana kadar nesnelerin örneğinin başlatılmaması ve böylece ilişkili satırların paylaşılmamasına neden olmasıdır.
Bu tür bir hücre özelleştirmesi paylaşılmayan satırlar gerektirdiğinden, 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.
Dekont
veya DataGridViewColumn öğesinden DataGridViewCell türetdiğinizde ve türetilmiş sınıfa yeni özellikler eklediğinizde, kopyalama işlemleri sırasında yeni özellikleri kopyalamak için yöntemini geçersiz kıldığınızdan Clone
emin olun. Temel sınıfın Clone
özelliklerinin yeni hücreye veya sütuna kopyalanmaları için temel sınıfın yöntemini de çağırmalısınız.
DataGridView denetimindeki hücreleri ve sütunları özelleştirmek için
türünden adlı
DataGridViewRolloverCell
yeni bir hücre sınıfı türetin DataGridViewTextBoxCell .public class DataGridViewRolloverCell : DataGridViewTextBoxCell {
Public Class DataGridViewRolloverCell Inherits DataGridViewTextBoxCell
}
End Class
sınıfındaki Paint yöntemini
DataGridViewRolloverCell
geçersiz kılın. Geçersiz kılmada, önce barındırılan metin kutusu işlevselliğini işleyen temel sınıf uygulamasını çağırın. Ardından imlecin konumunu (ekran koordinatlarında) istemci alanının koordinatlarına dönüştürmek için denetimin PointToClientDataGridView yöntemini kullanın. Fare koordinatları hücrenin sınırları içinde yer alırsa, iç dikdörtgeni ç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 Sub
Fare işaretçisi OnMouseEnter girdiğinde veya hücrelerden ayrıldığında
DataGridViewRolloverCell
hücreleri yeniden boyamaya zorlamak için sınıfındaki ve OnMouseLeave yöntemlerini geçersiz kı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 Sub
türünden adlı
DataGridViewRolloverCellColumn
yeni bir sınıf türetin DataGridViewColumn . Oluşturucuda, özelliğine yeniDataGridViewRolloverCell
bir nesne atayın CellTemplate .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
Kod örneğinin tamamı, ö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:
- System, System.Windows.Forms ve System.Drawing derlemelerine başvurular.
Ayrıca bkz.
.NET Desktop feedback
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin