【转】winform中怎样使DataGridView的某一列可以添加两个Button控件
2014-12-26 11:41
295 查看
转自:http://bbs.csdn.net/topics/330117383
问题已解决,思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码 第一个按钮模板列的代码: 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, |
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 |
maczhufengming maczhufengming 本版等级: | #33 得分:0回复于: 2010-03-02 15:32:36 之后在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; } } } |
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 |
aideng aideng 本版等级: | #34 得分:0回复于: 2010-03-03 09:21:25 收藏了,以后好好学习一下 |
对我有用[0] 丢个板砖[0] 引用 | 举报 | 管理 |
maczhufengming maczhufengming 本版等级: | #35 得分:0回复于: 2010-03-03 09:23:05 这是效果图 |
相关文章推荐
- winform中怎样使DataGridView的某一列可以添加两个Button控件
- WINFORM中怎样使DATAGRIDVIEW的某一列可以添加两个BUTTON控件
- winform中怎样使DataGridView的某一列可以添加两个Button控件
- winform中怎样使DataGridView的某一列可以添加两个Button控件
- Winform中向DataGridView控件添加数据
- 数据添加到datagridview (可以添加多行)
- winform下 的DataGridView动态添加列 的问题,待高手解决
- 将dataGridView中的添加/删除等修改保存至数据库(winform - VS2005)
- WinForm 2.0 有代码两个DataGridView实现Master/Details
- Winform设计时,怎样给DataGridView加合计行!!!
- 向DataGridView中添加新的一行数据,可以添加到最后一行或作为第一行
- WinForm 2.0 无代码两个DataGridView实现Master/Details
- C# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据;从Excel导入到DataGridView[转http://yuunagi.blogbus.com/logs/38361242.html]
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- winform 中的datagridview添加、修改、删除
- winform中DataGridView添加ComboBox的最终解决方案(点击ComboBox默认显示当前行的内容)
- winform 中的 datagridview 添加 progressbar列 和 calendar 列
- [VB.NET]怎样才可以右击选中datagridview控件中的单元格(1000分问题)
- WinForm中给DataGridView添加 自动编号