none
Datagridview column header merge issue RRS feed

  • Question

  • this is my code

    on button click i am binding grid with merge header column. i got this code from a site. if i click multiple times on button then column header height is getting increase lot for each click om button. i want column header height should not increase if i click multiple times on button. what to change in code?

    private void button1_Click(object sender, EventArgs e) {

                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;

    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"); this.dataGridView1.Columns.Add("AprWin", "Win"); this.dataGridView1.Columns.Add("AprLoss", "Loss"); this.dataGridView1.Columns.Add("MayWin", "Win"); this.dataGridView1.Columns.Add("MayLoss", "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); this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll); this.dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged); } private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) { Rectangle rtHeader = this.dataGridView1.DisplayRectangle; rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2; this.dataGridView1.Invalidate(rtHeader); } private void dataGridView1_Scroll(object sender, ScrollEventArgs e) { Rectangle rtHeader = this.dataGridView1.DisplayRectangle; rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2; this.dataGridView1.Invalidate(rtHeader); } private void dataGridView1_Paint(object sender, PaintEventArgs e) { string[] monthes = { "Emp Info", "", "", "", "" }; for (int j = 0; j < 10; ) { Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true); int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -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(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; } } 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; } }



    • Edited by Sudip_inn Saturday, November 9, 2019 6:42 PM
    Saturday, November 9, 2019 6:35 PM

Answers

  • I could fix it. here is sample code

            int i = 0;
            
            private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("f_2Q 2019", typeof(string));
                dt.Columns.Add("f_1Q 2019", typeof(string));
                dt.Columns.Add("r_2Q 2019", typeof(string));
                dt.Columns.Add("r_1Q 2019", typeof(string));
    
                dt.Rows.Add(new Object[]{
                    "Joy",
                    "2015.33",
                    "1124.00",
                    "1104.00",
                    "2124.00",
               });
    
                dt.Rows.Add(new Object[]{
                    "Amit",
                    "6654.33",
                    "1124.00",
                    "1104.00",
                    "0127.00",
               });
    
                dt.Rows.Add(new Object[]{
                    "Suman",
                    "9988.33",
                    "1124.00",
                    "4587.00",
                    "0127.00",
               });
    
                dataGridView1.DataSource = dt;
    
                for (int x = 0; x <= dataGridView1.Columns.Count - 1; x++)
                {
                    if (dataGridView1.Columns[x].HeaderText.Contains("f_") || dataGridView1.Columns[x].HeaderText.Contains("r_"))
                    {
                        var hname = dataGridView1.Columns[x].HeaderText.Replace("f_", string.Empty).Replace("r_", string.Empty);
                        dataGridView1.Columns[x].HeaderText = hname;
                    }
                }
    
                this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                if (i == 0)
                    this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
                i = 1;
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
                this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
                this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
                this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
                this.dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
            }
    
            private void dataGridView1_Paint(object sender, PaintEventArgs e)
            {
    
                int j = 1;
                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, false);
                int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width + w2 - 2;
                r1.Height = 46 / 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("Fundamental",
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                format);
    
                j += 2;
    
                r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, false);
                w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width + w2 - 2;
                r1.Height = 46 / 2 - 2;
                e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
                format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString("RDSS",
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                format);
            }
    
            private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
            {
                Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
                rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
                this.dataGridView1.Invalidate(rtHeader);
            }
    
            private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
            {
                Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
                rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
                this.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;
                }
            }

    • Marked as answer by Sudip_inn Saturday, November 9, 2019 7:08 PM
    Saturday, November 9, 2019 7:08 PM

All replies

  • Hi Sudip,

    Don't put all that grid setup code in a button click event handler, put it in the Form Load event handler.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, November 9, 2019 7:08 PM
    Moderator
  • I could fix it. here is sample code

            int i = 0;
            
            private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("f_2Q 2019", typeof(string));
                dt.Columns.Add("f_1Q 2019", typeof(string));
                dt.Columns.Add("r_2Q 2019", typeof(string));
                dt.Columns.Add("r_1Q 2019", typeof(string));
    
                dt.Rows.Add(new Object[]{
                    "Joy",
                    "2015.33",
                    "1124.00",
                    "1104.00",
                    "2124.00",
               });
    
                dt.Rows.Add(new Object[]{
                    "Amit",
                    "6654.33",
                    "1124.00",
                    "1104.00",
                    "0127.00",
               });
    
                dt.Rows.Add(new Object[]{
                    "Suman",
                    "9988.33",
                    "1124.00",
                    "4587.00",
                    "0127.00",
               });
    
                dataGridView1.DataSource = dt;
    
                for (int x = 0; x <= dataGridView1.Columns.Count - 1; x++)
                {
                    if (dataGridView1.Columns[x].HeaderText.Contains("f_") || dataGridView1.Columns[x].HeaderText.Contains("r_"))
                    {
                        var hname = dataGridView1.Columns[x].HeaderText.Replace("f_", string.Empty).Replace("r_", string.Empty);
                        dataGridView1.Columns[x].HeaderText = hname;
                    }
                }
    
                this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                if (i == 0)
                    this.dataGridView1.ColumnHeadersHeight = this.dataGridView1.ColumnHeadersHeight * 2;
                i = 1;
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
                this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
                this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
                this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
                this.dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged);
            }
    
            private void dataGridView1_Paint(object sender, PaintEventArgs e)
            {
    
                int j = 1;
                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, false);
                int w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width + w2 - 2;
                r1.Height = 46 / 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("Fundamental",
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                format);
    
                j += 2;
    
                r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, false);
                w2 = this.dataGridView1.GetCellDisplayRectangle(j + 1, -1, true).Width;
                r1.X += 1;
                r1.Y += 1;
                r1.Width = r1.Width + w2 - 2;
                r1.Height = 46 / 2 - 2;
                e.Graphics.FillRectangle(new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1);
                format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                e.Graphics.DrawString("RDSS",
                this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
                new SolidBrush(this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
                r1,
                format);
            }
    
            private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
            {
                Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
                rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
                this.dataGridView1.Invalidate(rtHeader);
            }
    
            private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
            {
                Rectangle rtHeader = this.dataGridView1.DisplayRectangle;
                rtHeader.Height = this.dataGridView1.ColumnHeadersHeight / 2;
                this.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;
                }
            }

    • Marked as answer by Sudip_inn Saturday, November 9, 2019 7:08 PM
    Saturday, November 9, 2019 7:08 PM
  • Hi Sudip, 

    Even though you said that you have found a solution, please be aware that the setup of the grid should be done in the Form Load event (as I replied earlier). 

    That includes all the code that you had in the Button1 Click event in your original post. I see that your revised code above (a different button click) includes setting up the grid's Paint, Scroll, etc. event handlers. That should not be in that button click either … put it in the Form Load. 

    I think that you will still have problems clicking Button2 multiple times (you will keep adding additional "subscriptions" to those event handlers).


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, November 9, 2019 8:54 PM
    Moderator