Colouring and Centering DataGridView Cells [modified]
-
HI All - I few days ago I posed a question about this and got an answer, but in the end this particular method did not work for me. I have managed to find a way to do it and I thought I would share it. Hope this helps someone. In the CellPainting event.......... 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 'This cell 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) If Not (e.Value Is Nothing) Then Dim g As Graphics = e.Graphics 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 :) See the article for the full subroutine code -- modified at 13:41 Saturday 20th January, 2007 David Loring !! Keep Music Live !!