C# DataGridView 重绘单元格按钮样式

1、重绘样式

我是把绘制方法写到了util类中,在页面方法中调用。 这样就会在单元格内出现两个按钮。
前提是:DataGridView 中的这一列的头的名字是:操作(下面用到,否则不匹配)

CellPainting的事件中调用Painting()方法:

 private void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
 {
     util.Painting(e, myDataGridView);
 }

样式如下:
在这里插入图片描述

2、调用按钮的事件,执行方法

在点击事件中,根据返回值,判断是调用了哪个方法,查看、修改、删除。

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事件,可能会使页面卡顿。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要实现DataGridView中指定单元格的合并功能,可以使用DataGridView的CellPainting事件来自定义单元格的绘制。 以下是一个示例,演示如何将DataGridView中指定的单元格合并为一个单元格: ```csharp private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // 指定要合并的单元格坐标 int mergeColumnIndex = 1; // 要合并的列索引 int mergeRowIndex = 1; // 要合并的行索引 // 只对指定单元格进行处理 if (e.RowIndex == mergeRowIndex && e.ColumnIndex == mergeColumnIndex) { // 获取指定单元格 DataGridViewCell curCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; // 获取要合并的单元格 DataGridViewCell mergeCell = dataGridView1.Rows[mergeRowIndex + 1].Cells[mergeColumnIndex]; // 判断指定单元格和要合并的单元格的值是否相同 if (curCell.Value != null && mergeCell.Value != null && curCell.Value.Equals(mergeCell.Value)) { // 要合并的单元格被合并到指定单元格 e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None; mergeCell.Visible = false; } } } ``` 在这个示例中,我们指定了要合并的单元格的坐标,然后在CellPainting事件中判断当前单元格是否为指定单元格。如果是,我们就获取要合并的单元格,判断其值是否和指定单元格的值相同。如果相同,我们将要合并的单元格的可见性设置为false,并将指定单元格的下边框样式设置为None,以达到合并单元格的效果。 需要注意的是,这种方法只能合并指定单元格和它下面的单元格,如果需要合并其他单元格,需要根据实际情况进行适当的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写代码的小R

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值