winform的DataGridView合并单元格

摘要:
privatevoiddataGridView1_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor);SolidBrushbackBrus

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor);
            SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor);
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int cellheight;
            int fontheight;
            int cellwidth;
            int fontwidth;
            int countU = 0;
            int countD = 0;
            int count = 0;
            // 对第1列相同单元格进行合并
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                cellheight = e.CellBounds.Height;
                fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
                cellwidth = e.CellBounds.Width;
                fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
                Pen gridLinePen = new Pen(gridBrush);
                string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                string curSelected = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value == null ? ""
                    : this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();
                if (!string.IsNullOrEmpty(curValue))
                {
                    for (int i = e.RowIndex; i < this.dataGridView1.Rows.Count; i++)
                    {
                        if (this.dataGridView1.Rows[i].Cells[0].Value.ToString().Equals(curValue))
                        {
                            this.dataGridView1.Rows[i].Cells[0].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

                            this.dataGridView1.Rows[i].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

                            countD++;

                        }

                        else
                        {
                            break;
                        }
                    }
                    for (int i = e.RowIndex; i >= 0; i--)
                    {
                        if (this.dataGridView1.Rows[i].Cells[0].Value.ToString().Equals(curValue))
                        {
                            this.dataGridView1.Rows[i].Cells[0].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

                            this.dataGridView1.Rows[i].Selected = this.dataGridView1.Rows[e.RowIndex].Selected;

                            countU++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    count = countD + countU - 1;
                    if (count < 2) { return; }
                }

                if (this.dataGridView1.Rows[e.RowIndex].Selected)
                {
                    backBrush.Color = e.CellStyle.SelectionBackColor;
                    fontBrush.Color = e.CellStyle.SelectionForeColor;
                }

                e.Graphics.FillRectangle(backBrush, e.CellBounds);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (countU - 1) + (cellheight * count - fontheight) / 2);

                if (countD == 1)
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    count = 0;
                }

                // 画右边线
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                e.Handled = true;
            }

        }

免责声明:文章转载自《winform的DataGridView合并单元格》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Js计算时间差Flink DataStream API下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

docker安装宝塔

主机的/home/www文件夹映射到docker容器的/www(注意:如果文件目录不存在,特权意味着在运行容器时,容器被授予特权,容器有权写入文件。然后问题来了……安装完成后,如果重新启动容器,容器宝塔会丢失吗?不,让我们试试:...

element ui设置表格表头高度和每一行的高度

.el-table__headertr,.el-table__headerth{padding:0;height:30px;line-height:30px;}.el-table__bodytr,.el-table__bodytd{padding:0;height:30px;line-height:30px;}...

ESXi挂载NFS共享存储

使用万兆交换机,ESXi使用NFS协议连接存储。本文介绍的是通过NFS协议挂载共享存储上的VS01卷,共享存储上已经赋予ESXi主机访问该卷的权限。...

WinForm 中 comboBox控件之数据绑定

作为列表类型,public class Info{public string Id{get;Name=“Li Si”};infoList.Add(info3);...

2.页面绘制和引入组件库uView

文本+背景色的形式,而不是横幅图的形式,可以节省未来的工作量。在index.vue中,关于开关的代码:EFGHIJKLMNOPQRSTUWXYZB˃DEFGHIJKLNNOPQRSTUVWXYZEFGHIJKLMNOPQRSTUVWXYZ导出默认值{data(){return{}},onLoad()},方法:{}}。横幅{width:100%;height:...

adb

ADB(AndroidDebugBridge)ANR(ApplicationNoResponding)ADB实际上是Android调试桥AndroidDebugBridge的缩写。adb是C/S体系结构的命令行工具。这里我们介绍一些常用的命令:adbdevices,获取设备列表和设备状态[xuxu:~]$adbdevicesList-devicesattac...