DataGridViewCellPaintingEventArgs Класс
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Предоставляет данные о событии CellPainting.
public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
- Наследование
Примеры
В следующем примере кода показано использование этого типа. Дополнительные сведения см. в разделе "Практическое руководство. Настройка внешнего вида ячеек в элементе управления DataGridView в Windows Forms".
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 вызывается для каждого DataGridViewCell , видимого на объекте DataGridView. Чтобы повысить производительность, задайте свойства в DataGridViewCellPaintingEventArgs свойстве, чтобы изменить внешний вид ячейки вместо прямого доступа к ячейке в ячейке DataGridView. Если вы вручную закрасили ячейку HandledEventArgs.Handled , задайте для свойства значение true. Если не задано HandledEventArgs.Handledtrueзначение, ячейка будет окрашена в настройки.
Конструкторы
Свойства
| Имя | Описание |
|---|---|
| AdvancedBorderStyle |
Возвращает стиль границы текущего DataGridViewCell. |
| CellBounds |
Получение границ текущего DataGridViewCell. |
| CellStyle |
Возвращает стиль ячейки текущего DataGridViewCell. |
| ClipBounds |
Возвращает область DataGridView , которая должна быть переопределена. |
| ColumnIndex |
Возвращает индекс столбца текущего DataGridViewCell. |
| ErrorText |
Возвращает строку, представляющую сообщение об ошибке для текущего DataGridViewCell. |
| FormattedValue |
Возвращает форматируемое значение текущего DataGridViewCell. |
| Graphics |
Возвращает используемый Graphics для рисования текущего DataGridViewCell. |
| Handled |
Возвращает или задает значение, указывающее, полностью ли обработчик событий обработал событие или должен ли система продолжать собственную обработку. (Унаследовано от HandledEventArgs) |
| PaintParts |
Части ячейки, которые должны быть окрашены. |
| RowIndex |
Возвращает индекс строки текущего DataGridViewCell. |
| State |
Возвращает состояние текущего DataGridViewCell. |
| Value |
Возвращает значение текущего DataGridViewCell. |
Методы
| Имя | Описание |
|---|---|
| Equals(Object) |
Определяет, равен ли указанный объект текущему объекту. (Унаследовано от Object) |
| GetHashCode() |
Служит хэш-функцией по умолчанию. (Унаследовано от Object) |
| GetType() |
Возвращает Type текущего экземпляра. (Унаследовано от Object) |
| MemberwiseClone() |
Создает неглубокую копию текущей Object. (Унаследовано от Object) |
| Paint(Rectangle, DataGridViewPaintParts) |
Закрашивает указанные части ячейки для области в указанных границах. |
| PaintBackground(Rectangle, Boolean) |
Закрашивает фон ячейки для области в указанных границах. |
| PaintContent(Rectangle) |
Закрашивает содержимое ячейки для области в указанных границах. |
| ToString() |
Возвращает строку, представляющую текущий объект. (Унаследовано от Object) |