问题已解决,思路是这样:分别创建三个新的按钮模板列,第一个显示删除图片,第二个显示编辑图片,第三个显示添加图片.看代码 第一个按钮模板列的代码: 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; } } }
Button的事件处理再哪里?怎么看不到
可以实现 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;
楼主你要的是不是这个软件??关键字: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
OnRowDataBound="gvPlanDrill_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:ImageButton ID="bntCommit" ImageUrl="~/Images/Commit.gif" runat="server" CommandName="commit"
CausesValidation="false" ToolTip="总结" CommandArgument='<%# Eval("PLAN_ID")%>' />
<asp:ImageButton ID="btnEdit" ImageUrl="~/Images/table_edit.png" runat="server" CommandName="ed"
CausesValidation="false" ToolTip="编辑" CommandArgument='<%# Eval("PLAN_ID")%>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
点编辑时,弹出一个Form窗体,把DataGridView中这一行的数据显示在弹出窗体相应的控件中。删除按钮是把DataGridView中的这一行数据删除掉
如果是做WINFORM , 改一下设计思路。 其实ASP.NET下把删除、编辑按扭放到GRID中,那是不得已而为之。
ASP.NET没有当前行这样的属性。WINFORM下有ToolStrip控件,把操作按扭放到工具栏中。你可以仿“资源管理器”或“IE”界面。
那样才是标准的WINDOWS界面风格。不要用浏览器下的界面设计风格,来设计WINDOWS应用程序。
up楼主是被要求一定那么做吗?如果不是的话建议换成lzsh0622所说的方法来实现吧,只要在按钮事件里添加个选中行的判断就能实现相同的功能了
if(dataGridView1.CurrentCell.OwningColumn.Name=="列名")
{
button1.visable=true;
button2.visable=true;button1.Left = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left;
button1.Top = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top;
button1.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Width;
this.dataGridView1.Controls.Add(button1);button2.Left = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left;
button2.Top = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top;
button2.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Width;
this.dataGridView1.Controls.Add(button2);}
else
{
button1.visable=false;
button2.visable=false;}
3.自己代码去优化下。
{
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((sender as Button).Text);
}
private void DataGridView_Scroll(object sender, ScrollEventArgs e)
{
}
// 改变DataGridView列宽时调整Button位置
private void DataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
}
第一个按钮模板列的代码:
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的后台代码如下:
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; }
}
}
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;
此产品属于产品集合。具体功能:这个“三合一”产品系列是为专业和企业开发者设计的,它包含了以下三个产品:3D Active Button Magic : 最新的3D按钮 ActiveX控件
3D Button API: 最新的3D按钮软件开发包(SDK)
3D Button Visual Editor: 最新的WYSIWYG(所见即所得)3D按钮编辑器 3D Button Suite