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

maczhufengming 2010-02-25 01:36:56
我要在DataGridView里面添加一列
这一列里面的cell是两个Button按钮,系统提供的DataGridViewButtonColumn模板列只有一个按钮,不满足我的要求。在网上查了一下,估记得自己创建一个模板列了,可能要分别重写DataGridViewColumn和DataGridViewCell这两个类。不知道那位大虾有这方面的代码??????谢谢了
...全文
6923 42 打赏 收藏 转发到动态 举报
写回复
用AI写文章
42 条回复
切换为时间正序
请发表友善的回复…
发表回复
qthl003 2012-08-02
  • 打赏
  • 举报
回复
楼主你要的是不是这个软件??



关键字:

3D Button Suite是一个包含了三个产品的套包控件(3D Active Button Magic、3D Button API和3D Button Visual Editor)。
此产品属于产品集合。

具体功能:

这个“三合一”产品系列是为专业和企业开发者设计的,它包含了以下三个产品:

3D Active Button Magic : 最新的3D按钮 ActiveX控件
3D Button API: 最新的3D按钮软件开发包(SDK)
3D Button Visual Editor: 最新的WYSIWYG(所见即所得)3D按钮编辑器



3D Button Suite
葡萄塘 2012-05-16
  • 打赏
  • 举报
回复
可以实现
IList<Test> list = new List<Test>();
Test item = new Test();
item.ID = 1;
item.Name = "aa";
list.Add(item);
Test itemI = new Test();
itemI.ID = 2;
itemI.Name = "bb";
list.Add(itemI);
dataGridView1.DataSource = list;
(this.dataGridView1.Columns[0] as DataGridViewButtonColumn).Text = "修改";
(this.dataGridView1.Columns[0] as DataGridViewButtonColumn).UseColumnTextForButtonValue = true;
kevin_水滴石穿 2012-01-29
  • 打赏
  • 举报
回复
不错,呵呵,学习了。。。
han_leng 2011-12-09
  • 打赏
  • 举报
回复
Button的事件处理再哪里?怎么看不到
VeryShooter 2011-06-30
  • 打赏
  • 举报
回复
图片是怎么加到按钮上去的?
sirzxj 2011-04-11
  • 打赏
  • 举报
回复
maczhufengming 2010-03-03
  • 打赏
  • 举报
回复
这是效果图
aideng 2010-03-03
  • 打赏
  • 举报
回复
收藏了,以后好好学习一下
maczhufengming 2010-03-02
  • 打赏
  • 举报
回复
之后在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;

}
}
}
maczhufengming 2010-03-02
  • 打赏
  • 举报
回复
问题已解决,思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码
第一个按钮模板列的代码:
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,
maczhufengming 2010-02-26
  • 打赏
  • 举报
回复
怎么没人了呢,自己顶
lzsh0622 2010-02-25
  • 打赏
  • 举报
回复
WinForm应用程序中,对于包含DataGridView的界面 ,学会使用 BindingNavigator控件 , 其它功能往后加按扭就行了。
maczhufengming 2010-02-25
  • 打赏
  • 举报
回复
谢谢大家,下班了,明天希望大家继续帮我想半法!!!!!!多谢了
tkscascor 2010-02-25
  • 打赏
  • 举报
回复
额. 我用webform的想法了, 错了.不好意思.
itliyi 2010-02-25
  • 打赏
  • 举报
回复
lz没必要这样 不比B/S 下每行两个按钮 一般这样就可以了
maczhufengming 2010-02-25
  • 打赏
  • 举报
回复
引用 25 楼 tkascor 的回复:
... ..  其实我不知道 放两个button有什么问题..  囧. 代码不写, 那就直接拉进去.变成模版列就可以了.

怎么直接拉进去啊
tkscascor 2010-02-25
  • 打赏
  • 举报
回复
... .. 其实我不知道 放两个button有什么问题.. 囧. 代码不写, 那就直接拉进去.变成模版列就可以了.
qqiuzaihui 2010-02-25
  • 打赏
  • 举报
回复
在下面的两个事件中重定位一下Button的位置:

        //  滚动DataGridView时调整Button位置
private void DataGridView_Scroll(object sender, ScrollEventArgs e)
{
}
// 改变DataGridView列宽时调整Button位置
private void DataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
}
maczhufengming 2010-02-25
  • 打赏
  • 举报
回复
引用 21 楼 qqiuzaihui 的回复:
添加一个DataGridView控件, 然后使用下面的代码试试:

C# codeprivatevoid Form1_Load(object sender, EventArgs e)
{this.dataGridView1.Columns.Add("a","a");this.dataGridView1.Columns.Add("b","b");this.dataGridView1.Columns.Add("c","c");for (int i=0; i<3; i++)this.dataGridView1.Rows.Add();for (int i=0; i<3; i++)
{
Button[] btn=new Button[2];
btn[0]=new Button();
btn[0].Text="one";
btn[1]=new Button();
btn[1].Text="two";this.dataGridView1.Controls.Add(btn[0]);this.dataGridView1.Controls.Add(btn[1]);
Rectangle rect=this.dataGridView1.GetCellDisplayRectangle(2, i,false);
btn[0].Size= btn[1].Size=new Size(rect.Width/2, rect.Height);
btn[0].Location=new Point(rect.Left, rect.Top);
btn[1].Location=new Point(rect.Left+ btn[0].Width, rect.Top);
btn[0].Click+=new EventHandler(CustomBtn_Click);
btn[1].Click+=new EventHandler(CustomBtn_Click);
}
}void CustomBtn_Click(object sender, EventArgs e)
{
MessageBox.Show((senderas Button).Text);
}


在有滚动条时,按钮不随着流动条一起动啊
maczhufengming 2010-02-25
  • 打赏
  • 举报
回复
我给大家传一个图吧,家看看!!!

就要求第一列那样
加载更多回复(21)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧