winform窗体DataGridView合并单元格处理

news/2025/2/28 15:55:49/文章来源:https://www.cnblogs.com/soulsjie/p/18293481

文本是使用SunnyUI的UIDataGridView控件进行演示的,同样适用于System.Windows.Forms.DataGridView控件

具体需求如下,下表是个成绩表,其中姓名、总分、平均分这三列信息重复,需要对数据表进行合并单元格处理。

 实现该需求需要两个步骤:

1.给表格添加单元格重绘事件

 在方法uiDataGridView1_CellPainting中添加代码将需要合并的列的单元格边框去掉

if (e.RowIndex >= 0 && (e.ColumnIndex == 0 || e.ColumnIndex == 3 || e.ColumnIndex == 4)){// 不显示单元格边框e.AdvancedBorderStyle.All = DataGridViewAdvancedCellBorderStyle.None;}

  

使用MergeCells方法,将指定单元格合并,原理是把要合并的单元格的内容清掉,然后在合并的中间行位置再将数值打出,实现合并的效果。

public void MergeCells(DataGridView dataGridView, int startRow, int endRow, int column, Color color){if (dataGridView.Columns.Count <= column || startRow < 0 || endRow >= dataGridView.Rows.Count || startRow >= endRow){return;}DataGridViewCell cell = dataGridView[column, startRow];string cellValue = cell.Value != null ? cell.Value.ToString() : "";for (int i = startRow; i <= endRow; i++){dataGridView[column, i].Value = null;}//在中间的行写值int valRowIndex = 0;int rows = endRow - startRow + 1;int halfRow = rows / 2;valRowIndex = startRow + halfRow;DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();textBoxCell.Value = cellValue;dataGridView[column, valRowIndex] = textBoxCell;for (int i = startRow; i <= endRow; i++){dataGridView[column, i].Style.BackColor = color;dataGridView[column, i].ReadOnly = true; // 设置合并后的单元格只读dataGridView[column, i].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; // 设置文本居中dataGridView[column, i].Style.Padding = new Padding(0); // 设置内边距为0,达到不显示边框的效果dataGridView[column, i].Style.SelectionBackColor = dataGridView[column, i].Style.BackColor; // 设置选中背景色与背景色一致}}

  

调用:

            //合并 张三MergeCells(uiDataGridView1, 0, 2, 0, Color.Pink);MergeCells(uiDataGridView1, 0, 2, 3, Color.Pink);MergeCells(uiDataGridView1, 0, 2, 4, Color.Pink);//合并 李四MergeCells(uiDataGridView1, 3, 5, 0, Color.Gray);MergeCells(uiDataGridView1, 3, 5, 3, Color.Gray);MergeCells(uiDataGridView1, 3, 5, 4, Color.Gray);

  

最终效果如下:

整个Form1.cs如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace MyForm
{public partial class Form1 : Form{private Random random = new Random();public Form1(){InitializeComponent();LoadData();//合并 张三MergeCells(uiDataGridView1, 0, 2, 0, Color.Pink);MergeCells(uiDataGridView1, 0, 2, 3, Color.Pink);MergeCells(uiDataGridView1, 0, 2, 4, Color.Pink);//合并 李四MergeCells(uiDataGridView1, 3, 5, 0, Color.Gray);MergeCells(uiDataGridView1, 3, 5, 3, Color.Gray);MergeCells(uiDataGridView1, 3, 5, 4, Color.Gray);}void LoadData(){List<Score> scores = new List<Score>();scores.Add(new Score() { Name = "张三", Subject = "数学", Mark = 68, Sum = 0, Avg = 0 });scores.Add(new Score() { Name = "张三", Subject = "语文", Mark = 100, Sum = 0, Avg = 0 });scores.Add(new Score() { Name = "张三", Subject = "英语", Mark = 35, Sum = 0, Avg = 0 });scores.Add(new Score() { Name = "李四", Subject = "数学", Mark = 26, Sum = 0, Avg = 0 });scores.Add(new Score() { Name = "李四", Subject = "语文", Mark = 85, Sum = 0, Avg = 0 });scores.Add(new Score() { Name = "李四", Subject = "英语", Mark = 48, Sum = 0, Avg = 0 });uiDataGridView1.RowCount = scores.Count();for (int i = 0; i < scores.Count(); i++){uiDataGridView1.Rows[i].Cells[0].Value = scores[i].Name;uiDataGridView1.Rows[i].Cells[1].Value = scores[i].Subject;uiDataGridView1.Rows[i].Cells[2].Value = scores[i].Mark;uiDataGridView1.Rows[i].Cells[3].Value = scores.Where(o=>o.Name== scores[i].Name).Select(o => o.Mark).Sum();uiDataGridView1.Rows[i].Cells[4].Value = Math.Round(scores.Where(o => o.Name == scores[i].Name).Select(o => o.Mark).Average(), 2);}}public void MergeCells(DataGridView dataGridView, int startRow, int endRow, int column, Color color){if (dataGridView.Columns.Count <= column || startRow < 0 || endRow >= dataGridView.Rows.Count || startRow >= endRow){return;}DataGridViewCell cell = dataGridView[column, startRow];string cellValue = cell.Value != null ? cell.Value.ToString() : "";for (int i = startRow; i <= endRow; i++){dataGridView[column, i].Value = null;}//在中间的行写值int valRowIndex = 0;int rows = endRow - startRow + 1;int halfRow = rows / 2;valRowIndex = startRow + halfRow;DataGridViewTextBoxCell textBoxCell = new DataGridViewTextBoxCell();textBoxCell.Value = cellValue;dataGridView[column, valRowIndex] = textBoxCell;for (int i = startRow; i <= endRow; i++){dataGridView[column, i].Style.BackColor = color;dataGridView[column, i].ReadOnly = true; // 设置合并后的单元格只读dataGridView[column, i].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; // 设置文本居中dataGridView[column, i].Style.Padding = new Padding(0); // 设置内边距为0,达到不显示边框的效果dataGridView[column, i].Style.SelectionBackColor = dataGridView[column, i].Style.BackColor; // 设置选中背景色与背景色一致}}private void uiDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){if (e.RowIndex >= 0 && (e.ColumnIndex == 0 || e.ColumnIndex == 3 || e.ColumnIndex == 4)){// 不显示单元格边框e.AdvancedBorderStyle.All = DataGridViewAdvancedCellBorderStyle.None;}}}public class Score{public string Name { get; set; }public string Subject { get; set; }public double Mark { get; set; }public double Sum { get; set; }public double Avg { get; set; }}}

  

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/741596.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

您的AI英语搭子!

本文由 ChatMoney团队出品 人工智能的发展,掀起了一波又一波AI浪潮,适合英语老师的AI软件也不断问世,老师们可以借助AI技术辅助自己的教学、帮助学生学习。你是否苦于想学习英语却没有语言环境,写英语内容时不知道语法和拼写是否正确,不知道表达方式是否足够的“Native”?…

Rocky Linux 9.4安装MySQL:使用RPM安装包方式

Rocky Linux 9.4安装MySQL:使用RPM安装包方式 一、安装环境安装环境如下:服务器:Rocky Linux 9.4安装版本:MySQL 8.0.38 二、安装过程和细节 1、在官网下载 RPM 安装包官网下载地址如下,这个地址里有各个版本的安装包,根据自己的版本选择,下载对应系统的安装包 https://…

Windows10设置任务栏时间显示秒数

Windows10设置任务栏时间显示秒数 操作步骤: 1、打开注册表 【Windows + R】键打开运行窗口,输入【regedit】,回车打开注册表编辑器。 2、新建注册表项 1)在注册表编辑器地址栏输入【计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanc…

BOSHIDA DC/AC电源模块在工业科技中的重要作用

BOSHIDA DC/AC电源模块在工业科技中的重要作用 随着工业科技的不断发展,直流(DC)和交流(AC)电源模块在工业生产和科技创新中起着重要作用。这些电源模块将电力转换为所需的形式和电压,以满足各种设备和系统的电力需求。在本文中,我们将探讨DC/AC电源模块在工业科技中的重…

遥遥领先!鲲鹏ARM架构下国产数据同步能力大幅提升16.9倍

在上篇文章《2.6倍!WhaleTunnel客户POC实景对弈DataX》发布之后,一个客户突然向我们控诉其苦DataX久矣,因为是在信创的鲲鹏ARM CPU上运行 ,每天同步需要很长时间,问我们可以不可以帮忙解决。 提起信创ARM CPU,大数据同仁们是又爱又恨,爱是因为价格真的便宜,恨是因为做大…

P3993 [BJOI2017] 同构 题解

Description 你有 \(n\) 个点,你可以在这 \(n\) 个点之间连无向边,两个点之间至多只能连一条边,也不允许连自环,问至多能连多少条边。 但这个问题的答案显然是 \(\frac{n(n-1)}{2}\) 条。所以有一个额外的限制,要求这个图不存在非平凡的自同构。 一个图 \(G\) 有非平凡的自…

极具未来感的京东.Vision来了!最潮的人已收藏!

没错!随着今天Apple Vision Pro国行版的正式发售,京东.Vision也正式和大家见面了!京东.Vision是什么?它是一款京东专为Apple Vision Pro用户打造的好玩、好逛、好买的沉浸式体验购物平台。首期,京东.Vision以“家”场景为核心,覆盖了包括三星电视、哈曼卡顿、MORRORART、…

2024 「全球软件研发技术大会】-刘兴东分享京东的AIGC革新之旅

大模型和开源的发展将带来全球软件研发技术的新变革,AI使代码自动化应用达到新水平,开源工具的云化和应用的AI化将促中国软件迎来新一轮的爆发。开发者正在迎接新一轮的技术浪潮变革。由CSDN和高端IT咨询和教育平台Boolan联合主办的2024年度「全球软件研发技术大会」于7 月4日…

VisualStudio各版本_MSC_VER和_MSC_FULL_VER宏定义值列表

这些值可以用于在C++中判断版本和C++特性支持情况。

利用SpringBoot+rabbitmq 实现邮件异步发送,保证100%投递成功

在之前的文章中,我们详细介绍了 SpringBoot 整合 mail 实现各类邮件的自动推送服务。 但是这类服务通常不稳定,当出现网络异常的时候,会导致邮件推送失败。 本篇文章将介绍另一种高可靠的服务架构,实现邮件 100% 被投递成功。类似的短信自动发送等服务也大体相同。 一、先来…

CS50P: 1. Conditionals

运算符 python中有 >= 和 <= ,其余和C一样 python支持 90 <= score <= 100C Python|| or& and布尔运算 True or False 选择语句 if if x < y:print("x is less than y") if x > y:print("x is greater than y") if x == y:print(&q…

Jenkins集成部署SpringBoot

Jenkins集成部署SpringBoot 1. 前言 随着业务的增长,需求也开始增多,每个需求的大小,开发周期,发布时间都不一致。基于微服务的系统架构,功能的叠加,对应的服务的数量也在增加,大小功能的快速迭代,更加要求部署的快速化,智能化。因此,传统的人工部署已经心有余而力不…