WinForm--dataGridView 合并标题/同一个单元格两个按钮

本文介绍了在WinForm的dataGridView中如何实现一列中显示两个文字按钮或图片按钮,以及如何合并标题。通过在CellPainting事件中绘制按钮,并在CellMouseClick事件中处理点击事件,可以创建自定义的UI效果。同时,通过设置列类型、隐藏边框和在Paint事件中合并标题,实现了视觉上的列标题合并。
摘要由CSDN通过智能技术生成

情景:在winform的dataGridView 中一个Column中显示两个按钮,本文将提及以下三种形式。

  1. 在一列中使用两个文字形式的按钮,得到的结果如下图的“Action”列。
  2. 在一列中显示两张按钮图片,得到的结果如下图的“Order First”列。
  3. 将按钮图标放在相邻的两列,合并标题,得到的结果如下图的“Order”列。

一.在一列中使用两个文字形式的按钮

注:参考文章https://www.cnblogs.com/aijiao/p/10038483.html

在datagridview的CellPainting事件中绘制两个文字按钮,写入如下代码:

 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Action")
                {
                    //set font style
                    StringFormat stringFormat = StringFormat.GenericDefault.Clone() as StringFormat;
                    stringFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    stringFormat.Alignment = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    stringFormat.Trimming = StringTrimming.EllipsisCharacter;
                    e.PaintBackground(e.CellBounds, false);
                    System.Drawing.Font font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    SizeF sizeDel = e.Graphics.MeasureString("Delete", font);
                    SizeF sizeLook = e.Graphics.MeasureString("Detail", font);

                    float fDel = sizeDel.Width / (sizeDel.Width + sizeLook.Width); 
                                                                                   
                    float fLook = sizeLook.Width / (sizeDel.Width + sizeLook.Width);

                    RectangleF rectDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height);
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值