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