DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy
DataGridView CellPainting Buggy DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
Go Back  Xtreme Visual Basic Talk > > > DataGridView CellPainting Buggy


Reply
 
Thread Tools Display Modes
  #1  
Old 04-04-2013, 04:40 PM
Gruff's Avatar
GruffDataGridView CellPainting Buggy Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default DataGridView CellPainting Buggy


Today I put together an app where I wanted to add Totals to the Column headers for certain fields.

The DataGridView does not support more than one fixed row at the top.
Casting about the web I came across the concept of doubling the header
height and then using CellPainting to owner draw the double header row.

Works a treat except for an odd graphics issue.
I draw a line through the center of each column header to visually separate the two rows.

The lines are drawn in all columns except for the last.
Text shows up in the last column header, but not the line.
Both are generated in the same loop, so I know it is not a range error.
(See Attached Picture)


I'm using e.CellBounds as my drawing rectangle area.

For now I worked around the issue by adding a dummy column and setting it to two pixels wide. The almost invisible dummy does not get the line,
but the column lines I want are drawn.

Is this a known issue for anyone?

Code:
Private Sub Grid_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Grid.CellPainting With e.Graphics If e.RowIndex = -1 Then Dim sf As New StringFormat Dim RX1 As Integer = e.CellBounds.X + 4 Dim RY1 As Integer = e.CellBounds.Y + 4 Dim RX2 As Integer = e.CellBounds.Width - 8 Dim RY2 As Integer = e.CellBounds.Height - 8 Dim newRect As New Rectangle(RX1, RY1, RX2, RY2) e.PaintBackground(e.ClipBounds, True) Dim X1 As Integer = e.CellBounds.X + 2 Dim X2 As Integer = e.CellBounds.Width - 2 Dim Y As Integer = e.CellBounds.Height \ 2 .DrawLine(SystemPens.ButtonShadow, X1, Y, X2, Y) Dim ColName As String = Grid.Columns(e.ColumnIndex).Name Dim sTotal As String = "" Select Case ColName Case "PPC_Quote" sTotal = MaterialTotal.ToString("c") Case "Total_Revenue" sTotal = RevenueTotal.ToString("c") Case "PPC_Hours" sTotal = HoursTotal.ToString("0.00") End Select sf.Alignment = StringAlignment.Far sf.LineAlignment = StringAlignment.Near .DrawString(sTotal, Grid.Font, Brushes.Blue, newRect, sf) sf.Alignment = StringAlignment.Center sf.LineAlignment = StringAlignment.Far .DrawString(Grid.Columns(e.ColumnIndex).HeaderCell.FormattedValue.ToString, Grid.Font, Brushes.Black, newRect, sf) e.Handled = True End If End With End Sub
Attached Images
File Type: png CellNoDrawLine.png (73.8 KB, 9 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 04-04-2013 at 05:31 PM.
Reply With Quote
  #2  
Old 04-05-2013, 01:21 PM
Gruff's Avatar
GruffDataGridView CellPainting Buggy Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Found the problem.

I used e.CellBounds.Width for the right coordinate of the line.
Should have been e.CellBounds.X + e.CellBounds.Width. Duh.!

The Line draws correctly now.

This does not answer why it drew full lines in all the other columns.
You would think they would be equally short.

In any case I can now move on to finish my project.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 04-05-2013 at 01:45 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
 
DataGridView CellPainting Buggy
DataGridView CellPainting Buggy
 
-->