如何在winforms中为网格的背景颜色添加填充
How to add padding to the background color of a grid in winforms
我正在尝试在 winforms 的 dataGridView 中使用填充,这样当我更改单元格的背景颜色 属性 时,它不会绘制整个单元格。然而,填充 属性 似乎只适用于单元格的文本,而不适用于背景颜色...有什么办法可以使它起作用吗?
我设法使用网格的 CellPainting 方法做到了
private void Grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// If it's not the column you want or it's the header
if (e.ColumnIndex != _grid.Columns["Deadline"].Index || e.RowIndex < 0)
return;
Color foreColor = e.CellStyle.ForeColor;
Color rectangleColor = ColorTranslator.FromHtml("#80AC7B");
Color backColor;
if (_grid.Rows[e.RowIndex].Selected)
backColor = ColorTranslator.FromHtml("#DFF5E0");
else
backColor = ColorTranslator.FromHtml("#FFFFFF");
// Rectangle to keep the background the same as the rest of the grid
Rectangle backgroundRect = new Rectangle(e.CellBounds.X,
e.CellBounds.Y, e.CellBounds.Width,
e.CellBounds.Height);
// Rectangle that will be colored above the first one
// Padding of 5 for all borders
Rectangle coloredRect = new Rectangle(e.CellBounds.X + 5,
e.CellBounds.Y + 5, e.CellBounds.Width - 10,
e.CellBounds.Height - 10);
using (
SolidBrush textBrush = new SolidBrush(foreColor),
backColorBrush = new SolidBrush(backColor),
coloredRectBrush = new SolidBrush(rectangleColor),
gridBrush = new SolidBrush(_grid.GridColor))
{
// Draw the background rectangle
e.Graphics.FillRectangle(backColorBrush, backgroundRect);
using (Pen gridLinePen = new Pen(gridBrush))
{
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
}
// Draw the colored rectangle
e.Graphics.FillRectangle(coloredRectBrush, coloredRect);
// Set center alignment for the text
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
// Draw the text in the cell above the colored rectangle
if (e.Value != null)
e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, textBrush, coloredRect, sf);
else
e.Graphics.DrawString("-", e.CellStyle.Font, textBrush, coloredRect, sf);
e.Handled = true;
}
}
我正在尝试在 winforms 的 dataGridView 中使用填充,这样当我更改单元格的背景颜色 属性 时,它不会绘制整个单元格。然而,填充 属性 似乎只适用于单元格的文本,而不适用于背景颜色...有什么办法可以使它起作用吗?
我设法使用网格的 CellPainting 方法做到了
private void Grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// If it's not the column you want or it's the header
if (e.ColumnIndex != _grid.Columns["Deadline"].Index || e.RowIndex < 0)
return;
Color foreColor = e.CellStyle.ForeColor;
Color rectangleColor = ColorTranslator.FromHtml("#80AC7B");
Color backColor;
if (_grid.Rows[e.RowIndex].Selected)
backColor = ColorTranslator.FromHtml("#DFF5E0");
else
backColor = ColorTranslator.FromHtml("#FFFFFF");
// Rectangle to keep the background the same as the rest of the grid
Rectangle backgroundRect = new Rectangle(e.CellBounds.X,
e.CellBounds.Y, e.CellBounds.Width,
e.CellBounds.Height);
// Rectangle that will be colored above the first one
// Padding of 5 for all borders
Rectangle coloredRect = new Rectangle(e.CellBounds.X + 5,
e.CellBounds.Y + 5, e.CellBounds.Width - 10,
e.CellBounds.Height - 10);
using (
SolidBrush textBrush = new SolidBrush(foreColor),
backColorBrush = new SolidBrush(backColor),
coloredRectBrush = new SolidBrush(rectangleColor),
gridBrush = new SolidBrush(_grid.GridColor))
{
// Draw the background rectangle
e.Graphics.FillRectangle(backColorBrush, backgroundRect);
using (Pen gridLinePen = new Pen(gridBrush))
{
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
}
// Draw the colored rectangle
e.Graphics.FillRectangle(coloredRectBrush, coloredRect);
// Set center alignment for the text
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
// Draw the text in the cell above the colored rectangle
if (e.Value != null)
e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, textBrush, coloredRect, sf);
else
e.Graphics.DrawString("-", e.CellStyle.Font, textBrush, coloredRect, sf);
e.Handled = true;
}
}