DataGridViewCellPaintingEventArgs 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 CellPainting 事件提供数据。
public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
- 继承
下面的代码示例演示了此类型的用法。 有关详细信息,请参阅如何:自定义 Windows 窗体 DataGridView 控件中单元格的外观。
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
if (this.dataGridView1.Columns["ContactName"].Index ==
e.ColumnIndex && e.RowIndex >= 0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the inset highlight box.
e.Graphics.DrawRectangle(Pens.Blue, newRect);
// Draw the text content of the cell, ignoring alignment.
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
Private Sub dataGridView1_CellPainting(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
Handles dataGridView1.CellPainting
If Me.dataGridView1.Columns("ContactName").Index = _
e.ColumnIndex AndAlso e.RowIndex >= 0 Then
Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
e.CellBounds.Width - 4, e.CellBounds.Height - 4)
Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
Dim gridBrush As New SolidBrush(Me.dataGridView1.GridColor)
Dim gridLinePen As New Pen(gridBrush)
Try
' Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
e.CellBounds.Bottom - 1)
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
e.CellBounds.Top, e.CellBounds.Right - 1, _
e.CellBounds.Bottom)
' Draw the inset highlight box.
e.Graphics.DrawRectangle(Pens.Blue, newRect)
' Draw the text content of the cell, ignoring alignment.
If (e.Value IsNot Nothing) Then
e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
StringFormat.GenericDefault)
End If
e.Handled = True
Finally
gridLinePen.Dispose()
gridBrush.Dispose()
backColorBrush.Dispose()
End Try
End If
End Sub
对于 CellPainting 在 上DataGridView可见的每个 DataGridViewCell ,都会引发 事件。 若要提高性能,请在 中 DataGridViewCellPaintingEventArgs 设置 属性以更改单元格的外观,而不是直接访问 中的 DataGridView单元格。 如果手动绘制单元格,请将 HandledEventArgs.Handled 属性设置为 true
。 如果未将 设置为 HandledEventArgs.Handledtrue
,则单元格将在自定义项上绘制。
Advanced |
获取当前 DataGridViewCell 的边框样式。 |
Cell |
获取当前 DataGridViewCell 的边界。 |
Cell |
获取当前 DataGridViewCell 的单元格样式。 |
Clip |
获取 DataGridView 中需要重新绘制的区域。 |
Column |
获取当前 DataGridViewCell 的列索引。 |
Error |
获取表示当前 DataGridViewCell 的错误消息的字符串。 |
Formatted |
获取当前 DataGridViewCell 的格式化值。 |
Graphics |
获取用于绘制当前 Graphics 的 DataGridViewCell。 |
Handled |
获取或设置一个值,该值指示事件处理程序是否已完整处理事件,或者系统是否应该继续本身的处理。 (继承自 HandledEventArgs) |
Paint |
要绘制的单元格部分。 |
Row |
获取当前 DataGridViewCell 的行索引。 |
State |
获取当前 DataGridViewCell 的状态。 |
Value |
获取当前 DataGridViewCell 的值。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
Get |
作为默认哈希函数。 (继承自 Object) |
Get |
获取当前实例的 Type。 (继承自 Object) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
Paint(Rectangle, Data |
绘制指定边界中相应区域的单元格的指定部分。 |
Paint |
绘制指定边界中相应区域的单元格背景。 |
Paint |
绘制指定边界中相应区域的单元格内容。 |
To |
返回表示当前对象的字符串。 (继承自 Object) |
产品 | 版本 |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9, 10 |