C# 窗体DataGridView单元格内多个按钮

在网上找了两种,基本一样,不同点是把button控件”画”在单元格,还是用画笔生成元素”画”在单元格

1、C# 窗体DataGridView单元格多个按钮控件

新建winform程序,添加一个datagridview

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Columns.Add("first", "first");
            this.dataGridView1.Columns.Add("second", "second");
            this.dataGridView1.Columns.Add("third", "third");
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            for (int i = 0; i < 15; i++)
                this.dataGridView1.Rows.Add();

            for (int i = 0; i < 15; i++)
            {
                Button[] btn = new Button[2];
                btn[0] = new Button();
                btn[0].Text = "操作1";
                btn[1] = new Button();
                btn[1].Text = "操作2";
                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);
        }

        //  滚动DataGridView时调整Button位置
        private void DataGridView_Scroll(object sender, ScrollEventArgs e)
        {
        }
        //  改变DataGridView列宽时调整Button位置
        private void DataGridView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
        }
    }

C#  窗体DataGridView单元格内多个按钮

============================================

2.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("点击查看按钮");
                }
            }

C#  窗体DataGridView单元格内多个按钮

转:https://blog.csdn.net/fsdad/article/details/121158819

https://blog.csdn.net/baidu_38995168/article/details/106519663

Original: https://www.cnblogs.com/fps2tao/p/16481451.html
Author: 与f
Title: C# 窗体DataGridView单元格内多个按钮

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/548922/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • crash命令 —— mod

    参考:https://crash-utility.github.io/help_pages/mod.html 用法: 加载指定内核模块的符号 mod -s <模块名字>…

    技术杂谈 2023年5月30日
    0109
  • Markdown相关语法介绍

    Markdown相关语法介绍 一、介绍 Markdown是一种轻量级标记语言,后缀是.md或者.markdown。 二、基础使用 标题 h1 ## h2 ### h3 #### h…

    技术杂谈 2023年6月21日
    0101
  • Redis常用概念及操作

    数据库 存储的位置 数据逻辑结构 MySQL、Oracle等数据库 硬盘 关系型数据 Redis 内存 key-value 支持的数据结构多:string、hash、set、lis…

    技术杂谈 2023年7月25日
    079
  • CloudCanal-2.0自定义代码实时加工能力(自定义实时ETL)说明与介绍

    简介 CloudCanal 2.0中我们将迎来一项重磅更新——自定义代码实时加工能力。自定义代码实时加工允许用户使用Java语言编写自定义的数据行处理逻辑,然后将代码jar包上传C…

    技术杂谈 2023年7月24日
    090
  • 用DirectX实现多视图渲染

    什么是多视图 一般的3D程序都只有一个视图,对应整个窗口的客户区。多视图就是在一个窗口中放置多个视图,以便从不同的角度观察模型或者场景。很多图形软件都有这个功能,比如大家熟知的3D…

    技术杂谈 2023年5月31日
    065
  • Nightingale 监控报警平台

    Nightingale 从官方的介绍是企业版的prometheus,从功能上的确是很不错的,我们基本上可以实现基于ui 灵活的管理prometheus 的报警处理 参考架构 Vic…

    技术杂谈 2023年5月30日
    0103
  • MySQL书写一个存储过程,修改指定表某个字段值如果相等就进行修改

    需求: 比如有一个表,attr_name类目名称如果相同,并且attr_name_chinese类目中文名如果A条记录存在,B条记录不存在,那么就通过记录id将B与A记录,进行同步…

    技术杂谈 2023年7月11日
    081
  • Delphi 之弹出气泡消息提示

    ///////////////////////———-参数说明—————&#8212…

    技术杂谈 2023年5月31日
    095
  • [教程] 【亲测可用】阿里云盘挂载变成你的电脑本地硬盘详细教程

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月31日
    0110
  • EMAS Serverless系列~4步教你快速搭建小程序

    体验简介 本实验基于 EMAS Serverless 的云函数、云数据库、云存储等云服务能力一站式快速开发小程序《私人云相册》。Demo 主要包括如下功能:1 相册管理2 上传相片…

    技术杂谈 2023年7月10日
    095
  • phpcms如何在前台文章列表显示来源

    phpcms的文章来源分为两种,一种是在后台来源中添加完成的,这种”来源”的相关数据存放于数据库的copyfrom表中,通过id和news_data表相关联…

    技术杂谈 2023年7月11日
    097
  • java面试题总结

    1,集合类面试题 arraylist和linkedlist的区别?底层实现?手写实现?线程安全吗以及原因? hashmap的底层实现?put()执行过程?put null时的执行过…

    技术杂谈 2023年7月24日
    064
  • wsl 加入右键菜单

    如果系统上安装两个版本wsl ubuntu,默认只有一个在右键菜单里,如果加另外一个1、找到ubuntu位置,复制2、打开注册表windows 与 R,输入regedit并运行,定…

    技术杂谈 2023年6月1日
    086
  • 跨域

    同源策略指三个相同:协议相同、域名相同、端口相同,有一个不同即非同源。 主域与子域、域名与域名对应的IP。都是非同源的 同源策略可以算是web安全的基石,没有同源策略就么有安全可言…

    技术杂谈 2023年5月31日
    081
  • Spring Cloud Loadbalancer

    Spring Cloud Loadbalancer—客户端负载均衡器 springcloud 2020.0.1 版本之后 删除了eureka中的ribbon,替代rib…

    技术杂谈 2023年7月25日
    076
  • []产品和成本效率总结提炼

    [原创]产品和成本效率总结提炼 1、个人的成本和效率: 对外(你的产品帮用户降低成本,提升效率)和对内(你的方法有没有更好的方案,如:天猫精灵,解决了必须要用手机控制设备) 2、组…

    技术杂谈 2023年5月30日
    0107
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球