今天在网上逛的时候,看到了一个童靴提的这个问题,看了帖子,发现楼主最终给出了自己的解决方案,感觉还不错,因此将帖子的内容整理了下,转出来了
解决方案的思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码
第一个按钮模板列的代码:
using System;
using System.Windows.Forms;
namespace 两列合并重绘列标题头
{
public class DataGridViewButtonColumnDel : DataGridViewColumn
{
public DataGridViewButtonColumnDel()
{
this.CellTemplate = new DataGridViewButtonCellDel();
this.HeaderText = "button";
}
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
namespace 两列合并重绘列标题头
{
public class DataGridViewButtonCellDel : DataGridViewButtonCell
{
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
Image _img = Properties.Resources.imgDelete_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y+3, _img.Width, _img.Height);
}
}
}
第二个按钮模板列的代码:
using System;
using System.Windows.Forms;
namespace 两列合并重绘列标题头
{
public class DataGridViewButtonColumnEdi : DataGridViewColumn
{
p