How to merge cells in datagridview?-VBForums
Results 1 to 9 of 9

Thread: How to merge cells in datagridview?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    1

    How to merge cells in datagridview?

    Hi,

    Is the following grid layout possible with DataGridView in VB.net? Kind of similiar to html table layout. I'm adding data cell by cell. However I'm not sure on how to get the layout for Group A & Group B Cells

    Code:
    -------------------------------------------------|
           | Item11      | Item12      | Item13      |
           |-----------------------------------------|
           | Item21      | Item22      | Item23      |
    GroupA |-----------------------------------------|
           | Item31      | Item32      | Item33      |
           |-----------------------------------------|
           | Item41      | Item42      | Item43      |
    -------------------------------------------------|
           | Item51      | Item52      | Item53      |
           |-----------------------------------------|
    GroupB | Item61      | Item62      | Item63      |
           |-----------------------------------------|
           |Item71      | Item72      | Item73       |
    -------------------------------------------------|
    Any help appreciated ...

  2. #2
    New Member
    Join Date
    Jan 2011
    Posts
    1

    Re: How to merge cells in datagridview?

    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

  3. #3
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: How to merge cells in datagridview?

    Is it possible to either:

    Assign a similar code to a button to merge user-selected cells;
    code the cell painting so that if two cells appear next to each other have the same content, they automatically "merge" as highlighted above?

    I'd be looking to merge across rows, eg, in the example below, BEFORE the code took action, row 1,2 and 3 of Column A all held the text ''DATA 123''

    ========================
    COLUMN A | COLUMN B |
    ------------------------------
    Row1.| |''col. b info''|
    Row2.| DATA 123 |more info |
    Row3.| |further info |

  4. #4

  5. #5
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: How to merge cells in datagridview?

    Hi Kevin - im coding in vb.net and am new at programming so am unable to convert ... any further suggestions? I've found some samples, eg...

    Code:
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
    
            Dim y As Integer
    
            For y = 0 To DataGridView1.Rows.Count - 1
    
                If (e.ColumnIndex = 0 And e.RowIndex = y) Then
    
                    If Not IsNumeric(DataGridView1(0, y).Value) Then   ' for instance i wanted to make something like band end merged with the next cell(s.t.h. like header band )
    
                        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single
    
                    End If
    
                    e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                End If
    
               
                If (e.ColumnIndex = 1 And e.RowIndex = y) Then
    
                    If y > 0 Then
    
                        If DataGridView1(1, y).Value = DataGridView1(1, y - 1).Value Then
                        Else
    
                            e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                        End If
                    End If
    
                End If
    
    
            Next
    
        End Sub
    This is fine, but ideally, I'd like the merged cells to look like cells that are merged in excel, ie, just one line of text, centred in the merged cells, rather than the same thing repeated in all rows...(hope that makes sense)

    help is greatly appreciated

  6. #6
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,934

    Re: How to merge cells in datagridview?

    Quote Originally Posted by MacShand View Post
    Hi Kevin - im coding in vb.net and am new at programming so am unable to convert ... any further suggestions? I've found some samples, eg...

    Code:
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
    
            Dim y As Integer
    
            For y = 0 To DataGridView1.Rows.Count - 1
    
                If (e.ColumnIndex = 0 And e.RowIndex = y) Then
    
                    If Not IsNumeric(DataGridView1(0, y).Value) Then   ' for instance i wanted to make something like band end merged with the next cell(s.t.h. like header band )
    
                        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single
    
                    End If
    
                    e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                End If
    
               
                If (e.ColumnIndex = 1 And e.RowIndex = y) Then
    
                    If y > 0 Then
    
                        If DataGridView1(1, y).Value = DataGridView1(1, y - 1).Value Then
                        Else
    
                            e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                        End If
                    End If
    
                End If
    
    
            Next
    
        End Sub
    This is fine, but ideally, I'd like the merged cells to look like cells that are merged in excel, ie, just one line of text, centred in the merged cells, rather than the same thing repeated in all rows...(hope that makes sense)

    help is greatly appreciated
    Is there a reason for adding cells one by one? Have you considered creating a DataTable, adding rows to the DataTable then assign the DataTable to the DataGridView DataSource as this would make this a lot easier. Otherwise you are in for a great mess of code to contend with.

    In the example below the rows added might simulate obtaining data from some source i.e. a text file etc.

    Code:
    Private Sub Demo()
        Dim MockedData As New DataTable()
        MockedData.Columns.AddRange(New DataColumn() _
            { _
                New DataColumn("Identifier", GetType(System.Int32)), _
                New DataColumn("ColumnA", GetType(System.String)), _
                New DataColumn("ColumnB", GetType(System.String)) _
            } _
        )
    
        MockedData.TableName = "Mocked"
    
        MockedData.Rows.Add(New Object() {1, "A", "Apples"})
        MockedData.Rows.Add(New Object() {1, "B", "Grapes"})
        MockedData.Rows.Add(New Object() {1, "C", "Pears"})
        MockedData.Rows.Add(New Object() {3, "D", "Cherries"})
        MockedData.Rows.Add(New Object() {3, "E", "Oranges"})
        MockedData.Rows.Add(New Object() {6, "F", "Prunes"})
        MockedData.Rows.Add(New Object() {6, "G", "Grapes"})
        MockedData.Rows.Add(New Object() {6, "H", "Oranges"})
        MockedData.Rows.Add(New Object() {6, "I", "Oranges"})
    
        DataGridView1.DataSource = MockedData
    End Sub
    I am not saying this is right for you since it sounds like you have taken a different path to load data but we need to be flexible and at least consider other options such as this.

  7. #7
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: How to merge cells in datagridview?

    The way the form works at run time, is that the user inputs the data to the datagridview in defined columns, but the rows will be changed so pre-loaded data will unfortunately not work. I appreciate you thinking of alternatives, it's great to get help and advice.

    Also, is the code you've suggested above for C# programming? I'm using Visual Basic 2010.

    Cheers

  8. #8
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,934

    Re: How to merge cells in datagridview?

    Quote Originally Posted by MacShand View Post
    Code:
    Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    
    
            Dim y As Integer
    
            For y = 0 To DataGridView1.Rows.Count - 1
    
                If (e.ColumnIndex = 0 And e.RowIndex = y) Then
    
                    If Not IsNumeric(DataGridView1(0, y).Value) Then   ' for instance i wanted to make something like band end merged with the next cell(s.t.h. like header band )
    
                        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single
    
                    End If
    
                    e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                End If
    
               
                If (e.ColumnIndex = 1 And e.RowIndex = y) Then
    
                    If y > 0 Then
    
                        If DataGridView1(1, y).Value = DataGridView1(1, y - 1).Value Then
                        Else
    
                            e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single
    
                        End If
                    End If
    
                End If
    
    
            Next
    
        End Sub

    Here is a consideration in regards to having logic in cell paint, place the following into your form with any amount of rows, at least two columns. At run time traverse thru the cells and each time you move cell to cell the Paint event is called, does this raise any red flags to you? It should.


    Code:
    Private Sub DataGridView1_Paint( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles DataGridView1.Paint
    
        Console.WriteLine(Now)
    End Sub

  9. #9
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,934

    Re: How to merge cells in datagridview?

    Quote Originally Posted by MacShand View Post
    The way the form works at run time, is that the user inputs the data to the datagridview in defined columns, but the rows will be changed so pre-loaded data will unfortunately not work. I appreciate you thinking of alternatives, it's great to get help and advice.
    Cheers
    I do not have time to code an example but the custom DataGridView can work for your situation.

    Basic steps:
    1. Assign an empty DataTable as the DataSource of the DataGridView.
    2. User inputs data, you add a new row to the DataTable
    3. Refresh the DataSource of the DataGridView

    Quote Originally Posted by MacShand View Post
    Also, is the code you've suggested above for C# programming? I'm using Visual Basic 2010.
    The link is in C Sharp but the code I supplied is VB.NET a conversion from the original code. Hence it works in VS2010 VB.NET

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.