DataGridView 显示和隐藏DataGridViewButtonCell按钮的办法

这是查看一个网友的帖子后找到的解决方案,在底部会贴上该帖子的链接。

这个解决方案是参考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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值