winform中怎样使DataGridView的某一列可以添加两个Button控件

今天在网上逛的时候,看到了一个童靴提的这个问题,看了帖子,发现楼主最终给出了自己的解决方案,感觉还不错,因此将帖子的内容整理了下,转出来了

解决方案的思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码
第一个按钮模板列的代码:
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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值