s
o
u
l
s
j
i
e

winform窗体DataGridView合并单元格处理

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

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

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

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

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

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

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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; // 设置选中背景色与背景色一致
            }
        }

  

调用:

1
2
3
4
5
6
7
8
//合并 张三
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如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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; }
    }
 
}

  

 

posted @   soulsjie  阅读(1279)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2018-07-10 SqlServer和Oracle修改表结构语句
你累吗?累就对了,当你觉得累时证明你在走上坡路!-----NotFoundObject - 2016-12-14 08:43
点击右上角即可分享
微信分享提示