我是把绘制方法写到了util类中,在页面方法中调用。 这样就会在单元格内出现两个按钮。
前提是:DataGridView 中的这一列的头的名字是:操作(下面用到,否则不匹配)
CellPainting的事件中调用Painting()方法:
private void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
util.Painting(e, myDataGridView);
}
在点击事件中,根据返回值,判断是调用了哪个方法,查看、修改、删除。
CellMouseClick的事件中调用Operation()方法:
private void DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int clickFlag = util.Operation(e, myDataGridView);
if (1 == clickFlag)
{
//删除操作
}
else if (2 == clickFlag)
{
//编辑操作
}
else if (3 == clickFlag)
{
//查看操作
}
}
下面是util类:
public static void Painting(DataGridViewCellPaintingEventArgs e, DataGridView dgView)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
if (dgView.Columns[e.ColumnIndex].HeaderText == "操作")
{
StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式
sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
e.PaintBackground(e.CellBounds, false);//重绘边框
//设置要写入字体的大小
System.Drawing.Font myFont = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);
SizeF sizeMod = e.Graphics.MeasureString("编辑", myFont);
SizeF sizeLook = e.Graphics.MeasureString("查看", myFont);
float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); //
float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width);
float fLook = sizeLook.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width);
//设置每个“按钮的边界”
RectangleF rectDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);
RectangleF rectMod = new RectangleF(rectDel.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height);
RectangleF rectLook = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fLook, e.CellBounds.Height);
e.Graphics.DrawString("删除", myFont, Brushes.Blue, rectDel, sf); //绘制“按钮”
e.Graphics.DrawString("编辑", myFont, Brushes.Blue, rectMod, sf);
e.Graphics.DrawString("查看", myFont, Brushes.Blue, rectLook, sf);
e.Handled = true;
e.Handled = true;
}
}
}
public static int Operation(DataGridViewCellMouseEventArgs e, DataGridView dgView)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
if (dgView.Columns[e.ColumnIndex].HeaderText == "操作")
{
Graphics g = dgView.CreateGraphics();
System.Drawing.Font myFont = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
SizeF sizeDel = g.MeasureString("删除", myFont);
SizeF sizeMod = g.MeasureString("编辑", myFont);
SizeF sizeLook = g.MeasureString("查看", myFont);
float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width);
float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width);
float fLook = sizeLook.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width);
Rectangle rectTotal = new Rectangle(0, 0, dgView.Columns[e.ColumnIndex].Width, dgView.Rows[e.RowIndex].Height);
RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height);
RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height);
RectangleF rectLook = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fLook, rectTotal.Height);
if (rectDel.Contains(curPosition))//删除
{
return 1;
}
else if (rectMod.Contains(curPosition))//编辑
{
return 2;
}
else if (rectLook.Contains(curPosition)) // 查看
{
return 3;
}
}
}
return 0;
}
这样重新绘制按钮可以实现该有的效果,但是和面一直启动或者刷新时,一直在调用CellPainting事件,可能会使页面卡顿。