Merge Datagriview columns headers

In this tutorial we will see how to merge Datagridview columns headers. For example we merge the columns indexed 1 and 2:

First we set the property ColumnHeadersHeightSizeMode of the Datagridview to EnableResizing.

Then add this code to Form_Load event:

dataGridView1.ColumnHeadersHeight = dataGridView1.ColumnHeadersHeight * 2;
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);


After that add codes to Paint, CellPainting, ColumnWidthChanged, and Scroll methods of the Datagridview:


private void dataGridView1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle r1 = dataGridView1.GetCellDisplayRectangle(1, -1, true);
            int w2 = dataGridView1.GetCellDisplayRectangle(2, -1, true).Width;
            r1.X += 1;
            r1.Y += 1;
            r1.Width = r1.Width + w2 - 2;
            r1.Height = r1.Height / 2 - 2;
            e.Graphics.FillRectangle(new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);

            StringFormat format = new StringFormat();

            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString("Merged Text", dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);
            
        }
        private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            Rectangle rtHeader = dataGridView1.DisplayRectangle;
            rtHeader.Height = dataGridView1.ColumnHeadersHeight / 2;
            dataGridView1.Invalidate(rtHeader);
        }
        private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            Rectangle rtHeader = dataGridView1.DisplayRectangle;
            rtHeader.Height = dataGridView1.ColumnHeadersHeight / 2;
            dataGridView1.Invalidate(rtHeader);

        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex > -1)
            {
                Rectangle r2 = e.CellBounds;
                r2.Y += e.CellBounds.Height / 2;
                r2.Height = e.CellBounds.Height / 2;
                e.PaintBackground(r2, true);
                e.PaintContent(r2);
                e.Handled = true;
            }

        }

This is the final result:
Merge Datagriview columns headers

In Datagridview_Paint event code we have this two statements:

Rectangle r1 = dataGridView1.GetCellDisplayRectangle(1, -1, true);
int w2 = dataGridView1.GetCellDisplayRectangle(2, -1, true).Width;

The first statement means drawing the rectangle starts from the column indexed 1 and the second means drawing rectangle ends in column indexed 2 . So if we want to merge 3 columns (columns indexed 0,1,2) the Paint method would be like:

Rectangle r1 = dataGridView1.GetCellDisplayRectangle(0, -1, true);
int w2 = dataGridView1.GetCellDisplayRectangle(1, -1, true).Width;
int w3 = dataGridView1.GetCellDisplayRectangle(2, -1, true).Width;

r1.X += 1;
r1.Y += 1;
r1.Width = r1.Width + w2 + w3;
r1.Height = r1.Height / 2 - 2;
e.Graphics.FillRectangle(new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);

StringFormat format = new StringFormat();

format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString("Merged Text", dataGridView1.ColumnHeadersDefaultCellStyle.Font,
      new SolidBrush(dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format);

This is the result:

Merge Datagriview columns headers

Merge Datagriview columns headers Merge Datagriview columns headers Reviewed by Bloggeur DZ on 11:59 Rating: 5

Aucun commentaire:

Fourni par Blogger.