WinForm_DataGridView一列多个按钮



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

  }
  }
}

已标记关键词 清除标记
本课程详细讲解了以下内容:     1.jsp环境搭建及入门、虚拟路径和虚拟主机、JSP执行流程     2.使用Eclipse快速开发JSP、编码问题、JSP页面元素以及request对象、使用request对象实现注册示例     3.请求方式的编码问题、response、请求转发和重定向、cookie、session执行机制、session共享问题      4.session与cookie问题及application、cookie补充说明及四种范围对象作用域      5.JDBC原理及使用Statement访问数据库、使用JDBC切换数据库以及PreparedStatement的使用、Statement与PreparedStatement的区别      6.JDBC调用存储过程和存储函数、JDBC处理大文本CLOB及二进制BLOB类型数据      7.JSP访问数据库、JavaBean(封装数据和封装业务逻辑)      8.MVC模式与Servlet执行流程、Servlet25与Servlet30的使用、ServletAPI详解与源码分析      9.MVC案例、三层架构详解、乱码问题以及三层代码流程解析、完善Service和Dao、完善View、优化用户体验、优化三层(加入接口和DBUtil)     1 0.Web调试及bug修复、分页SQL(Oracle、MySQL、SQLSERVER)      11.分页业务逻辑层和数据访问层Service、Dao、分页表示层Jsp、Servlet      12.文件上传及注意问题、控制文件上传类型和大小、下载、各浏览器下载乱码问题      13.EL表达式语法、点操作符和中括号操作符、EL运算、隐式对象、JSTL基础及set、out、remove      14.过滤器、过滤器通配符、过滤器链、监听器      15.session绑定解绑、钝化活化      16.以及Ajax的各种应用      17. Idea环境下的Java Web开发
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页