放在DrawCell事件里
'计算平均单价涨价幅度 If e.Col.Name = "平均单价" Then Dim rate As Decimal = 0 If e.Row("平均单价") <> 0 AndAlso e.Row("历史采购单价") <> 0 AndAlso e.Row("平均单价") <> e.Row("历史采购单价") Then rate = e.Row("平均单价") / e.Row("历史采购单价") - 1 End If If rate <> 0 Then e.StartDraw If rate < 0 Then '注意绘图的最大宽高度e.Width和e.Height,要-2,防止爆出单元格 Dim Width As Integer = (e.Width - 2) * ( - rate) e.Graphics.FillRectangle(Brushes.LightGreen, e.x + 1, e.y + 1, Width , e.Height - 2) Else If rate > 0 AndAlso rate < 1 Then Dim Width As Integer = (e.Width - 2) * rate e.Graphics.FillRectangle(Brushes.Tomato, e.x + 1, e.y + 1, Width , e.Height - 2) Else If rate >= 1 Then Dim Width As Integer = (e.Width - 2) * rate e.Graphics.FillRectangle(Brushes.Red, e.x + 1, e.y + 1, e.Width - 2, e.Height - 2) End If e.EndDraw End If End If