|
-
Jan 8th, 2015, 07:47 PM
#1
Thread Starter
PowerPoster
[RESOLVED] graphics / paint issue with datagridview
Hi all,
The task at hand was to add group headings within my data in the datagridview (i.e., I needed to merge cells in particular rows). Since this is not a native feature of the DGV, after hours of searching I found some code that does the trick. I modified that code to suit my needs, and it works great except for one wrinkle: it places extra, cut off text at the top of the DGV (in/above the column header area) and I can't figure out how to get rid of it. This screen shot shows the problem:
http://thevbprogrammer.com/dgv.png
The code used to accomplish the merging, in the DGV's Paint event, is as follows:
Code:
Private Sub dgvAvailItems_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles dgvAvailItems.Paint
Dim fnt As Font
Dim brush As Drawing.Brush
For intRowX As Integer = 0 To dgvAvailItems.Rows.Count - 1
If dgvAvailItems.Rows(intRowX).Cells(14).Value <> "" Then
If dgvAvailItems.Rows(intRowX).Cells(14).Value = "W" Then
fnt = New Font("Tahoma", 10, FontStyle.Italic + FontStyle.Bold, GraphicsUnit.Point)
brush = Brushes.RoyalBlue
Else
fnt = New Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point)
brush = Brushes.Black
End If
Dim rct As Rectangle = dgvAvailItems.GetRowDisplayRectangle(intRowX, True)
rct.Height -= 1
Dim s As SizeF = e.Graphics.MeasureString(dgvAvailItems.Rows(intRowX).Cells(1).Value, dgvAvailItems.Font)
Dim lefts As Integer = (rct.Width / 2) - (s.Width / 2)
Dim tops As Integer = rct.Top + ((rct.Height / 2) - (s.Height / 2))
e.Graphics.FillRectangle(Brushes.White, rct)
e.Graphics.DrawString(dgvAvailItems.Rows(intRowX).Cells(1).Value, fnt, brush, 2, tops)
End If
Next
End Sub
Any help would be appreciated. Thanks.
Last edited by BruceG; Jan 9th, 2015 at 11:26 AM.
Reason: resolved
"It's cold gin time again ..."
Check out my website here.
-
Jan 8th, 2015, 08:03 PM
#2
Re: graphics / paint issue with datagridview
Do you know what that text is? I can make out a few pieces. I thought it might be like the other blue text shown in the screen shot, but it is not. I see what looks like "worn with the" followed possibly by "summer". So, perhaps you know what is being drawn up there?
I'm thinking that what is happening is that you are getting a rct set to something you didn't expect for a row you didn't think would be shown. If this is always blue, then perhaps putting a breakpoint in the If statement where you set the bruch to blue would show you how often that fires. For each of those, step through to see what rct ends up being. You might find that one rct is being set terribly wrong. You might also note whether the text that is up there appears to change as you scroll the box.
Upon further examination, there also appears to be another line being drawn over the blue. That may mean something else. I believe you can get some information about the visible rows rather than just ALL the rows. Perhaps you could set up something in there that would skip the drawing code for rows that are not part of the visible set.
My usual boring signature: Nothing
 
-
Jan 8th, 2015, 08:24 PM
#3
Re: graphics / paint issue with datagridview
It looks as though you are trying to do your overrides in the DGV.Paint event. That may work, but there are row paint events that are intended for this. see: How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control
-
Jan 8th, 2015, 10:46 PM
#4
Thread Starter
PowerPoster
Re: graphics / paint issue with datagridview
Shaggy - The text appears to be the content of the items in the merged cells. There are 3 heading types, and I store an indicator in column 14 of the row ("G" for gender heading which be "Girls Specifications" or "Boys Specifications"; "S" for seasonal heading which will be something like "Winter Uniform Worn by Grades PK-8"; or "W" for "wear notes", which is the text I have in blue.) If column 14 is empty (""), it is a regular detail line (row). So it appears that the text of these headings are appearing not only where they are supposed to, but also, for whatever reason are appearing up at the top of the DGV, overwriting each other. Similar to what you suggested, I put a debug.print in my For loop to make sure the proper rows are being processed (and it seems that they are). Also, there are no hidden rows.
So I am open to further suggestion as far as what is going on and how to fix it.
TnTinMN - I have not had a chance to experiment with the example in your link, but that may be worth a shot. Ideally, however, if there is a fix for my existing code that would be preferable.
"It's cold gin time again ..."
Check out my website here.
-
Jan 8th, 2015, 11:32 PM
#5
Re: graphics / paint issue with datagridview
I meant hidden as in "scrolled off the top of the control". I noted that the scrollbar was halfway down the control, so it was clear that the control was scrolled down when the image was taken. The text partially visible is not the text from the similar row that is fully in the view area, so I figured that it was probably text from a row that was scrolled off the top, and which was still being painted. It was painted off the top of the control where it wouldn't normally be visible, but since you drew it at a larger than normal size (larger than the font for the normal rows), perhaps it extended down into the view area of the DGV.
My usual boring signature: Nothing
 
-
Jan 9th, 2015, 12:26 AM
#6
Thread Starter
PowerPoster
Re: graphics / paint issue with datagridview
You are on to something regarding scrolling. I have resizing capability on both the form itself, as well as the fact that the grid is in the top portion of a split container - and when I resize such that all the rows are visible (now I see what you meant) - the extraneous text at the top disappears. Similarly, when I resize it very small, more of the extraneous text is visible. So what do you think is happening there?
"It's cold gin time again ..."
Check out my website here.
-
Jan 9th, 2015, 12:38 AM
#7
Thread Starter
PowerPoster
[RESOLVED] Re: graphics / paint issue with datagridview
Woo-hoo - got it! The adjustment to the code is:
Code:
If dgvAvailItems.Rows(intRowX).Cells(14).Value <> "" AndAlso dgvAvailItems.Rows(intRowX).Displayed Then
Thanks, Shaggy, for putting me on the right path ...
Last edited by BruceG; Jan 9th, 2015 at 11:25 AM.
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|