这是查看一个网友的帖子后找到的解决方案,在底部会贴上该帖子的链接。
这个解决方案是参考https://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.cellpainting.aspx
后才写出来的。
贴上自己的代码如下:
#region dgv_finished单元格重绘事件 - 解决不想显示部分ButtonCell问题
private void dgv_finished_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex == 13 && dgv_finished["col_finished_state", e.RowIndex].Value.ToString() == "2")
{
dgv_finished[e.ColumnIndex, e.RowIndex].Tag = 2;
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.dgv_finished.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// 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 text content of the cell, ignoring alignment.
if (e.Value != null)
{
e.Graphics.DrawString("", e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
#endregion
这里的代码只是重绘了背景(应该是),加了2右边、底部2条线,判断如果单元格Value不为null就重绘一个空的文本。
具体可能大家的需求更加复杂,大家可以查看官网的例子自行需改。
参考网友的帖子的链接为:https://www.cnblogs.com/gxlxzys/archive/2011/06/09/2074472.html