今天在网上逛的时候,看到了一个童靴提的这个问题,看了帖子,发现楼主最终给出了自己的解决方案,感觉还不错,因此将帖子的内容整理了下,转出来了
解决方案的思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码
第一个按钮模板列的代码:
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;
}
}
}
分享到:
相关推荐
代码示例演示如何自定义 ... 为了演示此新单元格类型和列类型,父 DataGridView 中的每个 DataGridViewCheckBoxCell 的当前值确定同一行中 DataGridViewDisableButtonCell 的 Enabled 属性是 true 还是 false。
1.在解决方案中添加现有项目DLLFullPrint,然后在主工程中添加引用,引用这个项目。 2.在界面设计中,添加按钮PrintButton,双击它进入代码界面; 在代码文件中,首先使用using DLLFullPrint; 然后按钮点击事件中...
XPTable-winform很强大的表格控件,可以实现很多DATAGRIDVIEW没有的功能,如进度条,图片,下拉框等很多
这是c#中使用winform的datagridview控件添加按钮的demo 1、做自定义控件DataGridViewDisableButtonColumn。比系统自带的控件DatagridviewButtonColumn多了enabled属性 2、添加到程序的datagridview上。 3、根据数据...
写的是一个界面设计,但是里面牵扯到控件随窗口的变化而变化,同时还有pnael容器里的控件
近期学习C#的winform,整理的常用控件用法,入门级 包括button,datagridview,list,listview,checkBox,TreeGridView,MdiForm
C# winform 单击与双击事件同时存在例子 在wiform中单击与双击事件是不可以同时存在的。
C# .Net DataGridView控件使用大全(含有代码演示)
对数十个控件类进行了截图引导,希望在潜移默化中帮助读者开阔视野,提升能力,具体细节在实际运用再学习,textBox, button,toolbar,datagridview,treeview,label,radioButton,DataSet,ContextMenuStrip,...
1、新建一个WinForm程序,命名为BindingNavigatorMain,并拖入一个DataGridView控件及一个BindingNavigator控件。在BindingNavigator右下角弹窗中添加 一个Button(转到),BindingNavigator的样式如下: 2、...
实例108 计算两个矩形矩阵的乘积 129 第5章 面向对象编程技术 131 5.1 属性和方法 132 实例109 使用属性存储用户编号和姓名 132 实例110 通过定义方法求一个数的平方 133 实例111 使用重载方法实现不同类型数据的...
父窗体Form1 子窗体Form2 Form1中有一个datagridview控件和一添加按钮, Form2中有一个Text控件和一个保存按钮 要求点击Form1窗体上的添加按钮,弹出Form2, 再text里面输入内容,点击保存自动关闭Form2,刷新Form1...
页面一postback,它就显示页面的最顶端,怎样让它定位在某一位置? 如何保证页面刷新后的滚动条位置 清除网页历史记录,屏蔽后退按钮! 如何传值在2个页面之间 :要求不刷新父页面,并且不能用Querystring传值 Asp...
言提供了一个托管的运行时环境,使程序比以往更加稳定、安全。其特点有: 语言简洁。 保留了C++的强大功能 快速应用廾发功能 语言的自由性 强大的Web服务器控件。 支持跨平台 与XML相融合 如果之前没有其他话言的编程...
方案一:在窗体基类中每次加载并显示窗体时,会自动递归遍历含文本显示的控件(Button,CheckBox,GroupBox,Label,LinkLabel,TextBox,StatusStrip,TabPage,ToolStrip,RadioButton,DateTimePicker,DataGridView,...