Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / March 2006

Tip: Looking for answers? Try searching our database.

DataGridView CellPainting event

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 21 Mar 2006 23:12 GMT
I'm using the CellPainting event to customize how the cells are painted.
But when I use the DataGridViewCellPaintingEventArgs passed in, and call the
PaintContent method, it seems to ignore the Rectangle that I pass in.  It
just paints the text in the same place everytime.  Any ideas?
Kevin Spencer - 22 Mar 2006 00:17 GMT
Override the OnPaint event handler of the cell itself. This will require
creating an inherited cell class.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

> I'm using the CellPainting event to customize how the cells are painted.
> But when I use the DataGridViewCellPaintingEventArgs passed in, and call
> the PaintContent method, it seems to ignore the Rectangle that I pass in.
> It just paints the text in the same place everytime.  Any ideas?
johneevo - 30 Mar 2006 16:58 GMT
> I'm using the CellPainting event to customize how the cells are painted.
> But when I use the DataGridViewCellPaintingEventArgs passed in, and call the
> PaintContent method, it seems to ignore the Rectangle that I pass in.  It
> just paints the text in the same place everytime.  Any ideas?

Hi Greg,

I recent discovered this same problem.  If you don't mind using
reflection to hack the DataGridViewCellPaintingEventArgs class a little
you can try my work around (in C#)...

Object[] args = new Object[12] ;

args[0] = e.Graphics;
args[1] = e.ClipBounds;
args[2] = headerRect;  // your modified rectangle, passed as the
                      // cellBounds
args[3] = e.RowIndex;
args[4] = e.ColumnIndex;
args[5] = e.State;
args[6] = e.Value;
args[7] = e.FormattedValue;
args[8] = e.ErrorText;
args[9] = e.CellStyle;
args[10] = e.AdvancedBorderStyle;
args[11] = e.PaintParts;

e.GetType().InvokeMember("SetProperties",
                         BindingFlags.Instance |
                         BindingFlags.InvokeMethod |
                         BindingFlags.NonPublic,
                         null, e, args);

HTHS
Kevin Spencer - 30 Mar 2006 18:15 GMT
Here's an example from MSDN2:

http://msdn2.microsoft.com/en-US/library/system.windows.forms.datagridview.cellp
ainting.aspx


Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

>> I'm using the CellPainting event to customize how the cells are painted.
>> But when I use the DataGridViewCellPaintingEventArgs passed in, and call
[quoted text clipped - 31 lines]
>
> HTHS
johneevo - 30 Mar 2006 20:27 GMT
> Here's an example from MSDN2:
>
> http://msdn2.microsoft.com/en-US/library/system.windows.forms.datagridview.cellp
ainting.aspx
Hi Kevin,

Yes I did see that in the Help.  

But I also want to be able to support VisualStyles.  I was able to use
the VisualStyleRender to do this up to the point of getting the
SortArrow to draw.  I just could get that to work.

I tried to create a VisualStyleRender using the SortArrow StyleElement
(see code below) and just kept getting the error: "Given combination of
Class, Part, and State is not defined by the current visual style."  So
I finally gave up and decided to try to get Dot Net to draw it for me
while respecting the limits of my rectangle.

Here's the code snippet
VisualStyleRenderer vsr = new VisualStyleRenderer(
   VisualStyleElement.Header.SortArrow.SortedDown);
Kevin Spencer - 31 Mar 2006 13:59 GMT
Wish I could help you more, but honestly, I haven't played with this
particular item (VisualStyleElement and VisualStyleRenderer).

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

>> Here's an example from MSDN2:
>>
[quoted text clipped - 16 lines]
> VisualStyleRenderer vsr = new VisualStyleRenderer(
>    VisualStyleElement.Header.SortArrow.SortedDown);
johneevo - 31 Mar 2006 14:58 GMT
> Wish I could help you more, but honestly, I haven't played with this
> particular item (VisualStyleElement and VisualStyleRenderer).

That's OK.  Using the reflection trick is working for the most part just
need to use some more hacks to get the column header to display "Hot" or
not as appropriate.

The real reason for my post was more to try to help Greg out if he
didn't get his stuff working yet.

Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.