Problem with overriding Paint method for a DataGridViewButtonCell

P

Patrik

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);

}

}

}
 
L

Linda Liu [MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top