更改DataGridViewRow的轮廓 [英] Changing the outline of a DataGridViewRow
问题描述
如果满足特定条件,我想将 DataGridViewRow
的轮廓更改为蓝色以使其突出。我知道它不是 BackColor
,因为它会更改行的背景色。我试图将 ForeColor
更改为蓝色,但没有任何更改。我不相信这是选择样式,因为这种行为不是我想要的。有什么建议可以解决此问题?
item
下面是DataGridViewRow。
item.DefaultCellStyle.ForeColor =颜色。蓝色
item.DefaultCellStyle.BackColor =颜色。蓝色
感谢Jimi的帮助。
我已添加到datagridview.Paint
将visibleColumsWidth设置为Integer = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
Dim y作为Integer = dataGridView.GetCellDisplayRectangle(Column.DisplayIndex, rowIndex,True)。顶部
尺寸为新矩形的矩形(dataGridView.RowHeadersWidth,y,visibleColumsWidth,item.Height-1)
e.Graphics.DrawRectangle(Pens.Blue,rect)
您可以处理
如果您想绘制边框在单元格的边界内(或使用较粗的笔),将左 /上向下移动1个像素,并将高度缩短1个像素。
If a certain condition is met I want to change the outline of the DataGridViewRow
to blue to have it stand out. I know it isn't the BackColor
since it changes the row's background color. I have tried to change the ForeColor
to Blue but nothing changes. I wouldn't believe it is the selection styles since that behavior isn't what I am looking for. Any suggestions to get this behavior?
item
below is a DataGridViewRow.
item.DefaultCellStyle.ForeColor = Color.Blue
item.DefaultCellStyle.BackColor = Color.Blue
Thanks to help from Jimi.
I added to my datagridview.Paint
Dim visibleColumsWidth As Integer = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
Dim y As Integer = dataGridView.GetCellDisplayRectangle(Column.DisplayIndex, rowIndex, True).Top
Dim rect As New Rectangle(dataGridView.RowHeadersWidth, y, visibleColumsWidth, item.Height - 1)
e.Graphics.DrawRectangle(Pens.Blue, rect)
You can handle the RowPostPaint or RowPrePaint events to to paint the border of one or more Rows, based on specific conditions.
Here, I'm using the RowPostPaint
event handler, since you may have already some painting procedure in your DataGridView: RowPostPaint
allows to paint over an already drawn Row, redefining some details. There's no e.Handled
property to set, since the drawing has already been performed at this point.
The Handled
property is used to specify whether the event handler has completely handled the event or whether the system should complete the process.
- As usual, the bounding rectangle need to be adjusted, because of the way GDI+ draws this shape.
- You probably don't want to outline the row Header, which is included in the
e.RowBounds
value, so we need to subtract theDataGridView.RowHeadersWidth
from thee.RowBounds.Width
. - The outline should be applied only to the visible Columns, so we can use the
DataGridView.Columns.GetColumnsWidth()
method to get this measure, specifyingDataGridViewElementStates.Visible
as the Column state.
► Replace [Some Condition]
with the condition that should generate the outlining in your context.
Private Sub dataGridView1_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If [Some Condition] Then
Dim visibleColumsWidth As Integer = dataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
Dim rect = New Rectangle(dataGridView1.RowHeadersWidth, e.RowBounds.Top - 1, visibleColumsWidth, e.RowBounds.Height)
e.Graphics.DrawRectangle(Pens.Blue, rect)
End If
End Sub
If you instead want to outline a single cell, then handle the CellPainting event:
Here, we paint all parts but override the Cell border default drawing, using the e.Graphics.DrawRectangle()
method. The Cell bounding rectangle measure is slightly different, adapted to how the cell's internal borders are painted, so the new border overlaps the default one.
We also set e.Handled = True
, to notify that the drawing has been handled and there's no need to do anything else.
Private Sub dataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex < 0 OrElse e.ColumnIndex < 0 Then Return
If [Some Condition] Then
Dim rect = New Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height)
e.PaintBackground(e.CellBounds, True)
e.PaintContent(e.CellBounds)
e.Graphics.DrawRectangle(Pens.Red, rect)
e.Handled = True
End If
End Sub
This is the rendering with the current values:
If you prefer to draw the border inside the Cells' bounds (or use a thicker Pen), move Left/Top down 1 pixel and shorten the height by 1 pixel.
这篇关于更改DataGridViewRow的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!