0

I am looking for a way to connect multiple headers together in Visual Studio C# Forms.

The goal is that the Monday Columns Header 1-3 occupies the text as it is split into 3 headers underneath.

The other possibility I see is to make a second header, I just see the problem of customization that it always covers 3 fields.

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.RowCount = 2;
    dataGridView1.Columns[0].ReadOnly = true;
    dataGridView1.Columns[0].HeaderText = "Name";
    dataGridView1.Columns[1].HeaderText = "Monday";
    dataGridView1.Columns[1].Width = 30;
    dataGridView1.Columns[2].Width = 30;
    dataGridView1.Columns[3].Width = 30;
    dataGridView1.Rows[0].ReadOnly = true;
    dataGridView1.Rows[0].Cells[0].Value = "Thomas";
    dataGridView1.Rows[0].Cells[1].Value = "M1";
    dataGridView1.Rows[0].Cells[2].Value = "M2";
    dataGridView1.Rows[0].Cells[3].Value = "M3";
}

Programm1

Edit Resolution:

public partial class Form1 : Form
{
    DataGridView dataGridView1;
    TextFormatFlags centerTopflags =
        TextFormatFlags.HorizontalCenter | TextFormatFlags.Top | TextFormatFlags.PreserveGraphicsClipping;
    public Form1()
    {
        InitializeComponent();
        dataGridView1 = new DataGridView();
        dataGridView1.Height = this.Height - 100;
        dataGridView1.Dock = DockStyle.Top;
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.AllowUserToResizeRows = false;
        dataGridView1.RowHeadersVisible = false;
        this.Controls.Add(this.dataGridView1);
        Form1_Load();
    }

    private void Form1_Load()
    {
        
        dataGridView1.RowCount = 5;
        dataGridView1.ColumnCount = 25;
        dataGridView1.ColumnHeadersHeight = 35;
        dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;


        dataGridView1.Columns[0].ReadOnly = true;
        dataGridView1.Columns[0].HeaderText = "Name";
        dataGridView1.Columns[1].HeaderText = "M1";
        dataGridView1.Columns[2].HeaderText = "M2";
        dataGridView1.Columns[3].HeaderText = "M3";
        dataGridView1.Columns[4].HeaderText = "M1";
        dataGridView1.Columns[1].Width = 30;
        dataGridView1.Columns[2].Width = 30;
        dataGridView1.Columns[3].Width = 30;
        dataGridView1.Rows[0].Cells[0].Value = "Thomas";
        this.dataGridView1.Paint += new PaintEventHandler(dataGrid_Paint);

    }
    private void dataGrid_Paint(object sender, PaintEventArgs e)
    {
        DataGridView dataGridView = sender as DataGridView;
        DataGridViewCellStyle headerStyle = dataGridView.ColumnHeadersDefaultCellStyle;
        
        int colsLeft = dataGridView.Columns[1].Width + dataGridView.Columns[2].Width + dataGridView.Columns[3].Width;
        Rectangle CellRectangle1 = dataGridView.GetColumnDisplayRectangle(1, true);

        Rectangle mergeHeader = new Rectangle(CellRectangle1.X, CellRectangle1.Y, colsLeft, dataGridView.ColumnHeadersHeight);

        
        mergeHeader.Height = TextRenderer.MeasureText(e.Graphics, "Monday", headerStyle.Font, mergeHeader.Size, centerTopflags).Height;

        Rectangle clipRect = new Rectangle(
                dataGridView.RowHeadersWidth, 0,
                dataGridView.DisplayRectangle.Width - dataGridView.RowHeadersWidth,
                dataGridView.ColumnHeadersHeight
                );
        e.Graphics.SetClip(clipRect);

        SolidBrush brush = new SolidBrush(headerStyle.ForeColor);
        e.Graphics.FillRectangle(brush, mergeHeader);
        TextRenderer.DrawText(
            e.Graphics, "Monday",
            headerStyle.Font,
            mergeHeader,
            headerStyle.BackColor,
            centerTopflags
            );
         e.Graphics.ResetClip();

    }
}

Resolution

Nokman
  • 1
  • 2
  • 1
    see this link pls, https://stackoverflow.com/questions/16774966/how-to-merge-datagridview-cell-in-winforms – Mansur Kurtov Jul 05 '21 at 13:10
  • [Problem while scrolling merged Header Cells of a DataGridView](https://stackoverflow.com/a/67246419/7444103). IMO, it's not clear what you want to achieve. You can probably adapt that code, even if you want to replace the standard Headers' Text instead of adding a new one on top. – Jimi Jul 05 '21 at 13:21

0 Answers0