상세 컨텐츠

본문 제목

How do I show multi-layered column headers?

C#

by xarfox 2011. 5. 18. 21:53

본문

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


                |   January       |   February      |     March       |

        |  Win  | Loss   |  Win   | Loss   |  Win | Loss    |

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

Team1       |         |          |         |          |          |         |

Team2       |         |          |         |          |          |         |

TeamN       |         |          |         |          |          |         |

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




 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; )

            {

                //get the column header cell

                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);

 

                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;

            }

        }

 

 




Related threads:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1378821&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1442065&SiteID=1


[자료출처]
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e/

관련글 더보기