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

来源:互联网 发布:淘宝新店装修模板 编辑:程序博客网 时间:2024/04/30 05:15

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

解决方案的思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码
第一个按钮模板列的代码:
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
  {
  public DataGridViewButtonColumnEdi()
  {
  this.CellTemplate = new DataGridViewButtonCellEdi();
  this.HeaderText = "button";
  }
  }
}

using System;
using System.Windows.Forms;
using System.Drawing;
namespace 两列合并重绘列标题头
{
  public class DataGridViewButtonCellEdi : 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.imgEdit_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 DataGridViewButtonColumnAdd : DataGridViewColumn
  {
  public DataGridViewButtonColumnAdd()
  {
  this.CellTemplate = new DataGridViewButtonCellAdd();
  this.HeaderText = "button";
  }
  }
}

using System;
using System.Windows.Forms;
using System.Drawing;
namespace 两列合并重绘列标题头
{
  public class DataGridViewButtonCellAdd : 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.imgAdd_x16;
graphics.DrawImage(_img, cellBounds.Location.X + 5, cellBounds.Location.Y + 3, _img.Width, _img.Height);

  }
  }
}

上面的代码几乎是一样的,就是红色部份载入了不同的图片。
图片是通过资源文件引入项目的。方法是:打开资源管理器,展开“Properties”节点(这个节点默认是隐藏的)。双击Resources.resx,点击"添加资源",选择“添加现有资源”;添加三张图片:imgDelete_x16.png,imgEdit_x16,imgAdd_x16.png,

之后在form1上拖一个DataGridView1,给DataGridView1手动添加两列,分别是上面创建的DataGridViewButtonColumnDel,DataGridViewButtonCellEdi 这两个按钮模板列,ID分别为Column1,Column2;
form1的后台代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 两列合并重绘列标题头
{
  public partial class Form1 : Form
  {
  int top = 0;
  int left = 0;
  int height = 0;
  int width1 = 0;
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  BindDataGridView();
  HideCheckBoxCotrol();
  HideCheckBoxCotrol1();
  }
  /// <summary>
  /// 给DataGridView绑定测试数据
  /// </summary>
  private void BindDataGridView()
  {
  DataTable dt = new DataTable();
  dt.Columns.Add("Name", typeof(string));
  dt.Columns.Add("Age", typeof(string));

  for (int i = 0; i < 5; i++)
  {
  DataRow dr = dt.NewRow();
  dr[0] = i.ToString();
  dr[1] = i.ToString();
  dt.Rows.Add(dr);

  }

  this.dataGridView1.DataSource = dt.DefaultView;
   
  }

  /// <summary>
  /// 把第一列和第二列的头部重绘一下,绘成一个表头
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  {

  #region 重绘datagridview表头
  DataGridView dgv = (DataGridView)(sender);
  if (e.RowIndex == -1 && (e.ColumnIndex == 0 || e.ColumnIndex == 1))
  {
  if (e.ColumnIndex == 0)
  {
  top = e.CellBounds.Top;
  left = e.CellBounds.Left;
  height = e.CellBounds.Height;
  width1 = e.CellBounds.Width;
  }


  int width2 = this.dataGridView1.Columns[1].Width;

  Rectangle rect = new Rectangle(left, top, width1 + width2, e.CellBounds.Height);
  using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
  {
  //抹去原来的cell背景
  e.Graphics.FillRectangle(backColorBrush, rect);
  }
  using (Pen pen = new Pen(Color.White))
  {
  e.Graphics.DrawLine(pen, left + 1, top + 1, left + width1 + width2 - 1, top + 1);
  }
  using (Pen gridLinePen = new Pen(dgv.GridColor))
  {
  e.Graphics.DrawLine(gridLinePen, left, top, left + width1 + width2, top);
  e.Graphics.DrawLine(gridLinePen, left, top + height - 1, left + width1 + width2, top + height - 1);
  e.Graphics.DrawLine(gridLinePen, left, top, left, top + height);
  e.Graphics.DrawLine(gridLinePen, left + width1 + width2 - 1, top, left + width1 + width2 - 1, top + height);

  //计算绘制字符串的位置
  string columnValue = "";
  SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
  float lstr = (width1 + width2 - sf.Width) / 2;
  float rstr = (height / 2 - sf.Height);
  //画出文本框

  if (columnValue != "")
  {
  e.Graphics.DrawString(columnValue, e.CellStyle.Font,
  new SolidBrush(e.CellStyle.ForeColor),
  left + lstr,
  top + rstr,
  StringFormat.GenericDefault);
  }

  }
  e.Handled = true;


  }
  #endregion
  }
  /// <summary>
  /// 最后一行的第一列单元格中的DataGridViewButtonColumnDel按钮模板换
  /// 成系统的DataGridViewButtonCellAdd
  /// </summary>
  private void HideCheckBoxCotrol()
  {
  DataGridViewButtonCellAdd dvcType1 = new DataGridViewButtonCellAdd();
  dataGridView1.Rows[5].Cells["Column1"] = dvcType1;

  }
  /// <summary>
  /// 最后一行的第二列单元格中的DataGridViewButtonColumnEdi按钮模板换
  /// 成系统的DataGridViewTextBoxCell
  /// </summary>
  private void HideCheckBoxCotrol1()
  {
  DataGridViewCell dvcType = new DataGridViewTextBoxCell();
  dataGridView1.Rows[5].Cells["Column2"] = dvcType;

  }
  }
}