在 DataGridView 中为单元格着色和居中
描述了一种在 DataGridView 中着色和居中单元格的方法。
引言
我尝试寻找在 DataGridView
中着色和居中单元格的方法时遇到了很多问题。 我设法找到了一种方法,并想与大家分享。 在我的应用程序中,有三种颜色,并且包含这段代码,所以我包含了整个代码。 希望这能帮助到某人。
将此代码包含在 CellPainting
事件中... (在此实例中,网格的名称是 dgvAvail
)
Private Sub dgvAvail_CellPainting(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
Handles dgvAvail.CellPainting
If Me.dgvAvail.Columns(mint_AvailCapCol).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 redBackBrush As New SolidBrush(Color.Red)
Dim orangeBackBrush As New SolidBrush(Color.FromArgb(242, 181, 38))
Dim greenBackBrush As New SolidBrush(Color.LightGreen)
Dim gridBrush As New SolidBrush(Me.dgvAvail.GridColor)
Dim gridLinePen As New Pen(gridBrush)
Dim nsCellValue As Single = 0
Dim nsMaxValue As Single = 0
Dim sf As New StringFormat
sf.FormatFlags = StringFormatFlags.NoWrap Or StringFormatFlags.FitBlackBox
sf.LineAlignment = StringAlignment.Center
sf.Alignment = StringAlignment.Center
sf.Trimming = StringTrimming.None
If Information.IsDBNull(Me.dgvAvail.Rows(e.RowIndex).Cells(_
mint_AvailCapCol).Value) = True Then
nsCellValue = 0
Else
nsCellValue = _
Me.dgvAvail.Rows(e.RowIndex).Cells(mint_AvailCapCol).Value
End If
If Information.IsDBNull(Me.dgvAvail.Rows(_
e.RowIndex).Cells(mint_MaxCapCol).Value) = True Then
nsMaxValue = 0
Else
nsMaxValue = _
Me.dgvAvail.Rows(e.RowIndex).Cells(mint_MaxCapCol).Value
End If
Try
If nsCellValue = 0 Then
'This is a full outage and is shown Red
' Erase the cell.
e.Graphics.FillRectangle(redBackBrush, 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 text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
ElseIf nsCellValue > 0 And nsCellValue < nsMaxValue _
- (nsMaxValue * gCST_OrangeBoundaryValue / 100) Then
'This is a partial outage and is shown Orange
' Erase the cell.
e.Graphics.FillRectangle(orangeBackBrush, 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 text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
Else
'This is an OK situation and is shown Green
' Erase the cell.
e.Graphics.FillRectangle(greenBackBrush, 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 text content of the cell, ignoring alignment.
If Not (e.Value Is Nothing) Then
Dim r As System.Drawing.Rectangle
r.Width = (e.CellBounds.Right - 2) - (e.CellBounds.Left + 2)
r.Height = (e.CellBounds.Bottom - 2) - (e.CellBounds.Top + 2)
r.X = e.CellBounds.X + 2
r.Y = e.CellBounds.Y + 2
e.Graphics.TextRenderingHint = _
Drawing.Text.TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString(CStr(nsCellValue), _
e.CellStyle.Font, Brushes.Black, r, sf)
r = Nothing
End If
End If
e.Handled = True
Catch er As ApplicationException
MsgBox(er.Message)
Finally
gridLinePen.Dispose()
gridBrush.Dispose()
redBackBrush.Dispose()
greenBackBrush.Dispose()
orangeBackBrush.Dispose()
sf.Dispose()
End Try
End If
End Sub
尽情享受吧!!!