HighTechTalks DotNet Forums  

Problem with overriding Paint method for a DataGridViewButtonCell

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Problem with overriding Paint method for a DataGridViewButtonCell in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Patrik
 
Posts: n/a

Default Problem with overriding Paint method for a DataGridViewButtonCell - 02-01-2007 , 04:55 AM






Hi,

I am trying to build a custom button cell for my DataGridView. I found this code below that works fine if you want an ordinary button. What I want is the flatstyle look. The only thing I would like to customize is the size of the button. How should I implement the Paint if I want the flat style look?

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value,

object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

{

//base.Paint(graphics, clipBounds, cellBounds, rowIndex,

// elementState, value, formattedValue, errorText,

// cellStyle, advancedBorderStyle, paintParts);

// Draw the cell background, if specified.

if((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)

{

SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);

graphics.FillRectangle(cellBackground, cellBounds);

cellBackground.Dispose();

}

// Draw the cell borders, if specified.

if((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)

{

PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

}

//Calculate the area in which to draw the button.

Rectangle buttonArea = cellBounds;

Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);

buttonArea.X += buttonAdjustment.X;

buttonArea.Y += buttonAdjustment.Y;

buttonArea.Height -= buttonAdjustment.Height;

buttonArea.Width -= buttonAdjustment.Width;

//Draw the disabled button.

ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Default);

if(this.RowIndex >= 0)

{

//Draw the disabled button text.

if(this.FormattedValue is String)

{

TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, SystemColors.GrayText);

}

}

}


Reply With Quote
  #2  
Old   
Linda Liu [MSFT]
 
Posts: n/a

Default RE: Problem with overriding Paint method for a DataGridViewButtonCell - 02-02-2007 , 04:25 AM






Hi Patrik,

I have spent more than one hour researching on the ButtonRenderer class and
haven't found a way to draw a flat style button using the ButtonRenderer
class.

I have tried with VisualStyleElement and VisualStyleRenderer classes, but
unfortunately, they don't help in solving this problem either.

In fact, the DataGridViewButtonColumn class has provided a property named
FlatStyle, which gets or sets the flat-style appearance of the button cells
in the colum. We could set the value of FlatStyle property to Flat, so that
each button cell in that column has a flat style look.

If you'd like to draw your custom button cell with a flat style look, I
suggest that you draw it by yourself. You only need to draw the border of
the 'button'.

The following is a sample.

// comment out the below statement
//ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Default);
// use the following statements to draw the border of the button
using (Pen p = new Pen(cellStyle.ForeColor))
{
graphics.DrawRectangle(p, buttonArea.X, buttonArea.Y,
buttonArea.Width - 1, buttonArea.Height - 1);
}

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #3  
Old   
Patrik
 
Posts: n/a

Default Re: Problem with overriding Paint method for a DataGridViewButtonCell - 02-06-2007 , 04:54 AM



Thank you for a quick response!
/Patrik

"Linda Liu [MSFT]" <v-lliu (AT) online (DOT) microsoft.com> wrote

Quote:
Hi Patrik,

I have spent more than one hour researching on the ButtonRenderer class
and
haven't found a way to draw a flat style button using the ButtonRenderer
class.

I have tried with VisualStyleElement and VisualStyleRenderer classes, but
unfortunately, they don't help in solving this problem either.

In fact, the DataGridViewButtonColumn class has provided a property named
FlatStyle, which gets or sets the flat-style appearance of the button
cells
in the colum. We could set the value of FlatStyle property to Flat, so
that
each button cell in that column has a flat style look.

If you'd like to draw your custom button cell with a flat style look, I
suggest that you draw it by yourself. You only need to draw the border of
the 'button'.

The following is a sample.

// comment out the below statement
//ButtonRenderer.DrawButton(graphics, buttonArea,
PushButtonState.Default);
// use the following statements to draw the border of the button
using (Pen p = new Pen(cellStyle.ForeColor))
{
graphics.DrawRectangle(p, buttonArea.X, buttonArea.Y,
buttonArea.Width - 1, buttonArea.Height - 1);
}

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.