3

I'm using WinForms. How to add a row with horizontally merged cells to DataGridView? Something like WorkingTime and Touchdown Count cells on the following picture:

enter image description here

bairog
  • 3,143
  • 6
  • 36
  • 54
  • Please check this thread, it answers your question pretty much https://stackoverflow.com/questions/16774966/how-to-merge-datagridview-cell-in-winforms – Max Shevchenko Jul 10 '19 at 09:00
  • 1
    As I can see you advice me to set `e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None` inside `dataGridView1_CellPainting` event? That is not suitable for horisontal merge because first column (if I will place **Working Time** string into first cell of the row) will be autosized by its length. It will not look like merged column, but something [like that](https://i.imgur.com/qzLLG0k.jpg) – bairog Jul 10 '19 at 09:22
  • This is not well supported. To workaround you can hide some borders but the content will not be centered unless you ownerdraw it.. [See here](https://stackoverflow.com/questions/52484960/hide-specified-cell-borders-in-datagridview/52487741?r=SearchResults&s=2|30.7765#52487741) – TaW Jul 10 '19 at 09:24
  • @TaW - I've produced some code, that works for me (not only borders but centered content as well using custom `CellPainting` event). But I cannot answer my own question because you closed it. Open It please - so will be able to add full and correct solution. Thx. – bairog Jul 10 '19 at 10:41

1 Answers1

1

Here is working code for horizontal merge of the first row (both for centering cell content and proper border painting), initial code was found on MSDN:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    //mering all cells in a first row
    if (e.RowIndex == 0)
    {
        if (e.ColumnIndex == 0)
        {
            e.PaintBackground(e.ClipBounds, true);
            Rectangle r = e.CellBounds;

            for (int i = 1; i < (sender as DataGridView).ColumnCount; i++)
                r.Width += (sender as DataGridView).GetCellDisplayRectangle(i, 0, true).Width;

            r.Width -= 1;
            r.Height -= 1;

            using (SolidBrush brBk = new SolidBrush(e.CellStyle.BackColor))
            using (SolidBrush brFr = new SolidBrush(e.CellStyle.ForeColor))
            {
                e.Graphics.FillRectangle(brBk, r);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, brFr, r, sf);
            }

            e.Handled = true;
       }
       else
            if (e.ColumnIndex > 0)
            {
                using (Pen p = new Pen((sender as DataGridView).GridColor))
                {
                    //bottom line of a cell 
                    e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                    //right vertical line of a last cell in a row
                    if (e.ColumnIndex == (sender as DataGridView).ColumnCount - 1)
                        e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                }

                e.Handled = true;
            }
     }
}


private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
     //force redraw first row when scrolling
     for (int i = 0; i < (sender as DataGridView).ColumnCount; i++)
         (sender as DataGridView).InvalidateCell(i, 0);
}
bairog
  • 3,143
  • 6
  • 36
  • 54