各位大大,请教一个问题,怎么设置DataGridView中某一个单元格的边框颜色
各位大大,请教一个问题,怎么设置DataGridView中某一个单元格的边框颜色 --------------------编程问答-------------------- 这个问题高级了。只能重写 Cell 了http://blog.csdn.net/yanchao1023/article/details/5458803 --------------------编程问答-------------------- 在RowDataBound 时,添加cells的style属性就行 --------------------编程问答-------------------- 可以通过在绘制单元格时,重画单元格来实现.
下面vb.net代码将第一行第一列的边框改为了红色:
Private Sub dgv_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgvPrtLot.CellPainting
'判断单元格位置
If e.ColumnIndex = 0 And e.RowIndex = 0 Then
'指定边线颜色
Dim gridBrush As System.Drawing.Brush = New SolidBrush(Color.Red)
'指定背景颜色
Dim backColorBrush As System.Drawing.Brush = New SolidBrush(IIf(dgvPrtLot.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected _
, e.CellStyle.SelectionBackColor _
, e.CellStyle.BackColor))
'指定字体颜色
Dim foreColorBrush As System.Drawing.Brush = New SolidBrush(IIf(dgvPrtLot.Rows(e.RowIndex).Cells(e.ColumnIndex).Selected _
, e.CellStyle.SelectionForeColor _
, e.CellStyle.ForeColor))
Dim gridLinePen As System.Drawing.Pen = New Pen(gridBrush)
'清空表格
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
'画左线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left - 1, e.CellBounds.Top, e.CellBounds.Left - 1, e.CellBounds.Bottom)
'画右线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
'画上线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top - 1, e.CellBounds.Right - 1, e.CellBounds.Top - 1)
'画下线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
'根据对齐模式画数据
Dim drawFormat As New System.Drawing.StringFormat
Select Case e.CellStyle.Alignment
Case DataGridViewContentAlignment.MiddleLeft
drawFormat.Alignment = StringAlignment.Near
e.Graphics.DrawString(Trim("" & e.Value), e.CellStyle.Font, foreColorBrush, e.CellBounds.X + 2, e.CellBounds.Y + 5, drawFormat)
Case DataGridViewContentAlignment.MiddleCenter
drawFormat.Alignment = StringAlignment.Center
e.Graphics.DrawString(Trim("" & e.Value), e.CellStyle.Font, foreColorBrush, e.CellBounds.X + e.CellBounds.Width / 2, e.CellBounds.Y + 5, drawFormat)
Case DataGridViewContentAlignment.MiddleRight
drawFormat.Alignment = StringAlignment.Far
e.Graphics.DrawString(Trim("" & e.Value), e.CellStyle.Font, foreColorBrush, e.CellBounds.X + e.CellBounds.Width - 2, e.CellBounds.Y + 5, drawFormat)
End Select
e.Handled = True
End If
End Sub
补充:.NET技术 , VB.NET