博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView合并列标题
阅读量:4145 次
发布时间:2019-05-25

本文共 3177 字,大约阅读时间需要 10 分钟。

下面的示例实现如下形式的列标题合并。

-----------------------------------------------------------------

               |   January     |   February   |     March       |

               |  Win | Loss |  Win | Loss |  Win | Loss |
-----------------------------------------------------------------
Team1  |          |           |          |            |          |           |
Team2  |          |           |          |            |          |           |
Team3  |          |           |          |            |          |           |
-----------------------------------------------------------------

 

部分代码:

public partial class DgvColumnHeaderMerge : Form    {        public DgvColumnHeaderMerge()        {            InitializeComponent();        }        private void DgvColumnHeaderMerge_Load(object sender, EventArgs e)        {            this.dataGridView1.Columns.Add("JanWin", "Win");            this.dataGridView1.Columns.Add("JanLoss", "Loss");            this.dataGridView1.Columns.Add("FebWin", "Win");            this.dataGridView1.Columns.Add("FebLoss", "Loss");            this.dataGridView1.Columns.Add("MarWin", "Win");            this.dataGridView1.Columns.Add("MarLoss", "Loss");            for (int j = 0; j < this.dataGridView1.ColumnCount; j++)            {                this.dataGridView1.Columns[j].Width = 45;            }            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;            this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;            this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);            this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);        }        void dataGridView1_Paint(object sender, PaintEventArgs e)        {            string[] monthes = { "January", "February", "March" };            for (int j = 0; j < 6;)            {                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true); //get the column header cell                r1.X += 1;                r1.Y += 1;                r1.Width = r1.Width * 2 - 2;                r1.Height = r1.Height / 2 - 2;                e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);                StringFormat format = new StringFormat();                format.Alignment = StringAlignment.Center;                format.LineAlignment = StringAlignment.Center;                e.Graphics.DrawString(monthes[j/2],                    this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,                    new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),                    r1,                    format);                j += 2;            }        }        void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)        {            if (e.RowIndex == -1 && e.ColumnIndex > -1)            {                e.PaintBackground(e.CellBounds, false);                            Rectangle r2 = e.CellBounds;                r2.Y += e.CellBounds.Height / 2;                r2.Height = e.CellBounds.Height / 2;                e.PaintContent(r2);                e.Handled = true;            }        }    }

 

 

 

转载请注明出处!!!

 

 

你可能感兴趣的文章
组队总结
查看>>
TitledBorder 设置JPanel边框
查看>>
DBCP——开源组件 的使用
查看>>
抓包工具
查看>>
海量数据相似度计算之simhash和海明距离
查看>>
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>
DeepLearning tutorial(6)易用的深度学习框架Keras简介
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
流形学习-高维数据的降维与可视化
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>
网站用户登录系统设计——jsGen实现版
查看>>
第三方SDK:讯飞语音听写
查看>>
第三方SDK:JPush SDK Eclipse
查看>>
第三方开源库:imageLoader的使用
查看>>
自定义控件:飞入飞出的效果
查看>>
自定义控件:动态获取控件的高
查看>>
第三方开源库:nineoldandroid:ValueAnimator 动态设置textview的高
查看>>