Realy Nice DataGridView

NiceGridView.JPG

Introduction

The article demonstrates how to give .net DataGridView a nice look n' feel, by handling the DataGridView CellPaining Event.

Function to draw the column header.

Protected Sub DrawColumnHeader(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
Dim h As Integer = e.CellBounds.Height
Dim w As Integer = e.CellBounds.Width
Dim h1 As Integer = h * 0.4
Dim ct As New OfficeColorTable
Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, w, h1)
Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, h1, w, h - h1 + 1)
Dim lb1 As LinearGradientBrush = _
New LinearGradientBrush(r1, ct.ColumnHeaderStartColor, ct.ColumnHeaderMidColor1, Drawing2D.LinearGradientMode.Vertical)
Dim lb2 As LinearGradientBrush = _
New LinearGradientBrush(r2, ct.ColumnHeaderMidColor2, ct.ColumnHeaderEndColor, Drawing2D.LinearGradientMode.Vertical)
Dim p As Pen = New Pen(ct.GridColor, 1)
Dim frmt As StringFormat = New StringFormat
frmt.Alignment = StringAlignment.Center
frmt.FormatFlags = StringFormatFlags.DisplayFormatControl
frmt.LineAlignment = StringAlignment.Center
With e.Graphics
.FillRectangle(lb1, r1)
.FillRectangle(lb2, r2)
.DrawRectangle(p, e.CellBounds)
End With
End Sub

Function to draw DataGridView Cells.
Protected Sub DrawCell(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
Dim r1 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
Dim r2 As Rectangle = New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height)
Dim ct As New OfficeColorTable
Dim cellColor As Color
Dim borderColor As Color
Dim p As Pen
If e.State = 97 Or e.State = 105 Then
borderColor = ct.GridColor
cellColor = ct.ActiveCellColor
p = New Pen(borderColor, 1)
Else
borderColor = ct.GridColor
p = New Pen(borderColor, 1)
If e.State = 109 Then
cellColor = ct.ReadonlyCellColor
Else
cellColor = ct.DefaultCellColor
End If
End If
If e.ColumnIndex < 0 Then
cellColor = ct.ColumnHeaderMidColor2
End If
With e.Graphics
.FillRectangle(New SolidBrush(cellColor), e.CellBounds)
Dim rnd As New Renderer
If e.State = 97 Then
rnd.Fill3DRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
End If
If e.ColumnIndex < 0 Then
If e.State = 97 Then
rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007OrangeHover, e.Graphics)
Else
rnd.FillGradientRectangle(e.CellBounds, Renderer.RenderingMode.Office2007GrayHover, e.Graphics)
End If
End If
.DrawRectangle(p, e.CellBounds)
.DrawRectangle(p, New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, Me.Height))
End With
End Sub
来自:http://www.ueow.com

转载于:https://www.cnblogs.com/junzhongxu/archive/2008/10/06/1304428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值