CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Posts
    53

    Lightbulb DataGridView Merging Cells

    I have seen a large number of people post questions regarding merging cells of a DataGridView, so I thought I would take a stab at it. Here is the Merged Cell Class.. Attached is the demo project, and image.

    For vertical merging, goto www.windowsforms.net and download the "Outlook Calender Look and Feel" sample.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Drawing.Drawing2D;
    
    
    namespace DataGridView_MergeDemo
    {
        public class HMergedCell : DataGridViewTextBoxCell
        {
            private int m_nLeftColumn = 0;
            private int m_nRightColumn = 0;
    
            /// <summary>
            /// Column Index of the left-most cell to be merged.
            /// This cell controls the merged text.
            /// </summary>
            public int LeftColumn
            {
                get
                {
                    return m_nLeftColumn;
                }
                set
                {
                    m_nLeftColumn = value;
                }
            }
    
            /// <summary>
            /// Column Index of the right-most cell to be merged
            /// </summary>
            public int RightColumn
            {
                get
                {
                    return m_nRightColumn;
                }
                set
                {
                    m_nRightColumn = value;
                }
            }
    
            protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
            {
                try
                {
                    int mergeindex = ColumnIndex - m_nLeftColumn;
                    int i;
                    int nWidth;
                    int nWidthLeft;
                    string strText;
    
                    Pen pen = new Pen(Brushes.Black);
    
                    // Draw the background
                    graphics.FillRectangle(new SolidBrush(SystemColors.Control), cellBounds);
    
                    // Draw the separator for rows
                    graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Left, cellBounds.Bottom - 1, cellBounds.Right, cellBounds.Bottom - 1);
    
                    // Draw the right vertical line for the cell
                    if (ColumnIndex == m_nRightColumn)
                        graphics.DrawLine(new Pen(new SolidBrush(SystemColors.ControlDark)), cellBounds.Right - 1, cellBounds.Top, cellBounds.Right - 1, cellBounds.Bottom);
    
                    // Draw the text
                    RectangleF rectDest = RectangleF.Empty;
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;
    
                    // Determine the total width of the merged cell
                    nWidth = 0;
                    for (i = m_nLeftColumn; i <= m_nRightColumn; i++)
                        nWidth += this.OwningRow.Cells[i].Size.Width;
    
                    // Determine the width before the current cell.
                    nWidthLeft = 0;
                    for (i = m_nLeftColumn; i < ColumnIndex; i++)
                        nWidthLeft += this.OwningRow.Cells[i].Size.Width;
    
                    // Retrieve the text to be displayed
                    strText = this.OwningRow.Cells[m_nLeftColumn].Value.ToString();
    
                    rectDest = new RectangleF(cellBounds.Left - nWidthLeft, cellBounds.Top, nWidth, cellBounds.Height);
                    graphics.DrawString(strText, new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rectDest, sf);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }
            }
    
        }// class
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2008
    Posts
    1

    Re: DataGridView Merging Cells

    hi do you have a vb.net version of this code? thanks.

  3. #3
    Join Date
    Jun 2008
    Posts
    1

    Question Re: DataGridView Merging Cells

    Do you still need the vb version of that code?

  4. #4
    Join Date
    Mar 2009
    Location
    Delhi
    Posts
    1

    Question Re: DataGridView Merging Cells

    Hi epensado

    I Really need that code in Vb.net,
    Please do the needfull.

    Thanks

  5. #5
    Join Date
    Jan 2011
    Posts
    1

    Re: DataGridView Merging Cells

    Suppose one want to merge Cell 2 and 3 in Row 0 of a Datagrid(dg_ESC) then type following code in Cell Painting event of the Datagrid


    Private Sub dg_ESC_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dg_ESC.CellPainting

    If e.RowIndex = 0 AndAlso (e.ColumnIndex = 2 Or e.ColumnIndex = 3) Then
    Using gridBrush As Brush = New SolidBrush(Me.dg_ESC.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
    Using gridLinePen As Pen = New Pen(gridBrush)
    ' Clear cell
    e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
    'Bottom line drawing
    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
    'top line drawing
    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Top)
    'Drawing Right line
    If e.ColumnIndex = 3 Then
    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
    End If
    'Inserting text
    If e.ColumnIndex = 3 Then
    e.Graphics.DrawString(CType(e.Value, String), e.CellStyle.Font, Brushes.Black, e.CellBounds.X - (e.CellBounds.X / 4), e.CellBounds.Y + 5)
    End If
    e.Handled = True
    End Using
    End Using
    End If

    This solve's the problem...

    Thanking

    Rakesh Sharma,

    Jodhpur INDIA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured