C#窗体实现DataGridView一列多个按钮

代码:

 private void DataGridViewEmail_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号,与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               this.DataGridViewEmail.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                     this.DataGridViewEmail.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                     this.DataGridViewEmail.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

        }

        private void DataGridViewEmail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.DataGridViewEmail.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("微软雅黑", 9F, 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.Black, rectDel, sf); //绘制“按钮”
                    e.Graphics.DrawString("修改", myFont, Brushes.Black, rectMod, sf);
                    e.Graphics.DrawString("查看", myFont, Brushes.Black, rectLook, sf);
                    e.Handled = true;
                }
            }

        }

        private void DataGridViewEmail_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标
                if (this.DataGridViewEmail.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    Graphics g = this.DataGridViewEmail.CreateGraphics();
                    System.Drawing.Font myFont = new System.Drawing.Font("宋体", 9F, 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, this.DataGridViewEmail.Columns[e.ColumnIndex].Width, this.DataGridViewEmail.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))//删除
                        MessageBox.Show("点击删除按钮");
                    else if (rectMod.Contains(curPosition))//修改
                        MessageBox.Show("点击修改按钮");
                    else if (rectLook.Contains(curPosition))//查看
                        MessageBox.Show("点击查看按钮");
                }
            }

 

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你想在 DataGridView 的操作列添加多个按钮,你可以使用 DataGridViewButtonColumn 控件来创建一个新列。在该列,每个单元格都将包含一个或多个按钮,用户可以单击这些按钮来执行特定的操作。 以下是一个示例代码,演示如何创建一个包含两个按钮DataGridViewButtonColumn: ```csharp // 创建一个新的 DataGridViewButtonColumn 控件 DataGridViewButtonColumn column = new DataGridViewButtonColumn(); column.HeaderText = "操作"; // 设置列标题 column.Name = "btnColumn"; // 设置列名称 // 设置第一个按钮的属性 column.Text = "编辑"; column.UseColumnTextForButtonValue = true; // 设置第二个按钮的属性 DataGridViewButtonCell cell = new DataGridViewButtonCell(); cell.Value = "删除"; column.CellTemplate = cell; // 将新列添加DataGridView dataGridView1.Columns.Add(column); ``` 在上面的代码,我们首先实例化了一个新的 DataGridViewButtonColumn 控件,并设置了它的标题和名称。接着,我们设置了第一个按钮的属性,将其文本设置为“编辑”,并将 UseColumnTextForButtonValue 属性设置为 true,这样按钮单击时就会触发 DataGridView 的 CellClick 事件。然后,我们创建了一个新的 DataGridViewButtonCell 对象,并将其值设置为“删除”,并将其分配给新列的模板单元格。最后,我们将新列添加DataGridView 的列集合。 当用户单击任何一个按钮时,DataGridView 的 CellClick 事件将会被触发。你可以在该事件根据按钮的 Text 或 Value 属性来确定用户单击的是哪个按钮,并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

试行

祝您生活愉快!

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

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

打赏作者

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

抵扣说明:

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

余额充值