none
MERGING CELLS IN DATAGRIDVIEW

    Question

  • Hi all,

    Is it possible to merge cells(rows) in DatagridView And How?

    Thanx

    Tuesday, February 21, 2006 9:57 AM

Answers

  • The DataGridView does not have any support for merging cells. You can custom paint content across cells, but apart from that you would have to do a lot of work and custom coding to produce merged editable cells.

     

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"

    Tuesday, February 21, 2006 6:47 PM
  • Hi here is what I did with my datagrid,

    Create datagridview with i.e. 5 columns and 5 rows.  Then add code below to paint event of DATAGRIDVIEW

    Dim fnt As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Dim rct1 As New Rectangle((DataGridView1.GetColumnDisplayRectangle(0, True).X), (DataGridView1.GetColumnDisplayRectangle(0, True).Y + DataGridView1.Columns(0).HeaderCell.ContentBounds.Height + 8), DataGridView1.GetColumnDisplayRectangle(0, True).Width - 1, (DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Top - DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Height))
    e.Graphics.FillRectangle(Brushes.White, rct1)
    e.Graphics.DrawString(
    "VERTICAL" & vbCrLf & "TEXT", fnt, Brushes.Black, rct1)
    Dim rct As Rectangle = DataGridView1.GetRowDisplayRectangle(3, True)
    rct.Height -= 1
    Dim s As SizeF = e.Graphics.MeasureString("HORINZONTAL TEXT", DataGridView1.Font)
    Dim lefts As Integer = (rct.Width / 2) - (s.Width / 2)
    Dim tops As Integer = rct.Top + ((rct.Height / 2) - (s.Height / 2))
    e.Graphics.FillRectangle(Brushes.White, rct)
    e.Graphics.DrawString(
    "HORINZONTAL TEXT", fnt, Brushes.Black, 2, tops)


    From code above you will have first column as verticaly merged and 3rd row as horinzontaly merged. Hope this helps....
    P.S. For custom coding you will need to figure it out....

    Ismar

    Friday, March 17, 2006 8:04 AM
  • Hello,

    It is possible but you need to do tons of codes to achieve that. Painting event should only be ok if you want to merge content within control. Maybe the best for you would be to look for some custom components such as componentone true dbgrids or custom flex grid control. They might have solution for you....

    Regards, 

    Friday, March 17, 2006 1:33 PM

All replies

  • The DataGridView does not have any support for merging cells. You can custom paint content across cells, but apart from that you would have to do a lot of work and custom coding to produce merged editable cells.

     

    -mark

    DataGridView Program Manager

    Microsoft

    This post is provided "as-is"

    Tuesday, February 21, 2006 6:47 PM
  • Hi there Mark

    Thank you for reply. But I would like to ask you additional question;

    I was able to marge cells by Paint event but in my datagridview I have button and combobox columns. After painting event the controls in the rows are not visible but still they are accessible if user clicks on particular column. My question is how do I remove single combo box or button from a cell? I tried to do it by

    DataGridView1.Rows(3).Frozen = True

    DataGridView1.Rows(3).Cells(2).ReadOnly = True

    but it did not give results. Still there are controls attached to the cells.

    Wednesday, February 22, 2006 9:41 AM
  • i would like to know that as well...

    anyone an idea?

    Thursday, March 02, 2006 8:59 AM
  • Hello,

    I was only able to manage to disable rows by putting

    DataGridView.Rows(i).ReadOnly=true or DataGridView.Rows(i).Frozen=true

    but stil there are controls within the cell.......

    Thursday, March 02, 2006 9:58 AM
  • Hi ismar,

     

    I am also having the same problem.. Will u please tell me how to merge cells in a datagridview..

     

    How and what to do with that paint of method????

    Thanks in advance.

    Any help is appreciated

     

    Friday, March 17, 2006 5:20 AM
  • Hi here is what I did with my datagrid,

    Create datagridview with i.e. 5 columns and 5 rows.  Then add code below to paint event of DATAGRIDVIEW

    Dim fnt As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Dim rct1 As New Rectangle((DataGridView1.GetColumnDisplayRectangle(0, True).X), (DataGridView1.GetColumnDisplayRectangle(0, True).Y + DataGridView1.Columns(0).HeaderCell.ContentBounds.Height + 8), DataGridView1.GetColumnDisplayRectangle(0, True).Width - 1, (DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Top - DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Height))
    e.Graphics.FillRectangle(Brushes.White, rct1)
    e.Graphics.DrawString(
    "VERTICAL" & vbCrLf & "TEXT", fnt, Brushes.Black, rct1)
    Dim rct As Rectangle = DataGridView1.GetRowDisplayRectangle(3, True)
    rct.Height -= 1
    Dim s As SizeF = e.Graphics.MeasureString("HORINZONTAL TEXT", DataGridView1.Font)
    Dim lefts As Integer = (rct.Width / 2) - (s.Width / 2)
    Dim tops As Integer = rct.Top + ((rct.Height / 2) - (s.Height / 2))
    e.Graphics.FillRectangle(Brushes.White, rct)
    e.Graphics.DrawString(
    "HORINZONTAL TEXT", fnt, Brushes.Black, 2, tops)


    From code above you will have first column as verticaly merged and 3rd row as horinzontaly merged. Hope this helps....
    P.S. For custom coding you will need to figure it out....

    Ismar

    Friday, March 17, 2006 8:04 AM
  • Hi thanks for ur reply..

     

    What i want is that i have 2 row headers in my control. say Rowheader0 and rowHeader1. i want to split the Rowheader0 in to two headers like x1 and x2.. Is it possible to do that?????

     

    Please share ur ideas..

    Thanks in advance

    Friday, March 17, 2006 10:41 AM
  • Hello,

    It is possible but you need to do tons of codes to achieve that. Painting event should only be ok if you want to merge content within control. Maybe the best for you would be to look for some custom components such as componentone true dbgrids or custom flex grid control. They might have solution for you....

    Regards, 

    Friday, March 17, 2006 1:33 PM
  • Hi,

    In FlexGrid there is a property called Fixed Cols and Fixed Rows which will used for setting fixed rows and fixed Columns.(ie both primary and secondary). Is there any option is there in DataGridView(C# 2005). Or any idea abt how to do that??????

     

    Thanks in advance

     

    Please Share ur ideas

    Friday, March 24, 2006 11:21 AM
  • Hello sir,

      Where should i write your given Merge cell code means in which event exactly?

    can u pass me any code to merge cells in datagridview ?

     

    thnx in advance

     

     

    Friday, August 11, 2006 5:26 AM
  • Dear ,

        When I press the right arrow, The grid is not selecting the merged cells, it selects only first cell, bcoz it call each time

    RowPostPaint event to paint merge cell .Bcoz of this it is not showing all selected merge cells,

       Please help me When I press the right arrow,It shoulld select all merge cells ?

    Thanks in advance

    Tuesday, August 22, 2006 8:25 AM
  •  

    Dear Sir,

        When I press the right arrow, The grid is not selecting the merged cells, it selects only first cell, bcoz it call each time

    RowPostPaint event to paint merge cell .Bcoz of this it is not showing all selected merge cells,

       Please help me When I press the right arrow,It shoulld select all merge cells ?

    Please pass me ur code to MErger cells in Datagridview.

     

    Thanks in advance

    Tuesday, August 22, 2006 8:28 AM
  • Hi

           Ismar  I want to merge the 3rd row in which only the 4th n 5th cell should be merged n not the whole row. how is it possible please help me.

     

     

    Thursday, February 01, 2007 6:32 AM
  • Hello,

    It is nice to see that people keep asking question on this issue and the small fraction of code I provided was helpful to some of you. But this is only small part of custom coding to get really nice formated and merged datagridview... Anyways I dont think that I might be very helpful, since in many instances you need to figure it out how to make things in a way you want, but I can only provide small tips how to solve these kind of questions:

    Shailendra;

    In you paint event  before you paint the row you can check whether row is select or no. If yes then paint it to blue if not then paint it in white and so on. Look at the code bellow:

    Dim fnt As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Dim rct1 As New Rectangle((DataGridView1.GetColumnDisplayRectangle(2, True).X), (DataGridView1.GetColumnDisplayRectangle(2, True).Y + DataGridView1.Columns(2).HeaderCell.ContentBounds.Height + 8), DataGridView1.GetColumnDisplayRectangle(2, True).Width - 1, (DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Top - DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Height))
    e.Graphics.FillRectangle(Brushes.White, rct1)
    e.Graphics.DrawString(
    "VERTICAL" & vbCrLf & "TEXT 3rd MERGED COLUMN", fnt, Brushes.Black, rct1)
    Dim rct As Rectangle = DataGridView1.GetRowDisplayRectangle(3, True)
    rct.Height -= 1
    Dim s As SizeF = e.Graphics.MeasureString("HORINZONTAL TEXT", DataGridView1.Font)
    Dim lefts As Integer = (rct.Width / 2) - (s.Width / 2)
    Dim tops As Integer = rct.Top + ((rct.Height / 2) - (s.Height / 2))
    If DataGridView1.Rows(DataGridView1.CurrentRow.Index).Selected = True Then

    e.Graphics.FillRectangle(Brushes.Blue, rct)
    e.Graphics.DrawString(
    "HORINZONTAL TEXT", fnt, Brushes.White, 2, tops)
    Else
    e.Graphics.FillRectangle(Brushes.White, rct)
    e.Graphics.DrawString(
    "HORINZONTAL TEXT", fnt, Brushes.Black, 2, tops)
    End If

     

    Irshad_ip:

     Still I believe you should figure it on your own... It should be so easy just repleace all zeros(0) in rct1 to 2 (3rd) column and add another rectangle such as rct2 and do it same as rct1 just instead of 2 in your constructor put 3:

    Dim fnt As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Dim rct1 As New Rectangle((DataGridView1.GetColumnDisplayRectangle(2, True).X), (DataGridView1.GetColumnDisplayRectangle(2, True).Y + DataGridView1.Columns(2).HeaderCell.ContentBounds.Height + 8), DataGridView1.GetColumnDisplayRectangle(2, True).Width - 1, (DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Top - DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Height))
    Dim rct2 As New Rectangle((DataGridView1.GetColumnDisplayRectangle(3, True).X), (DataGridView1.GetColumnDisplayRectangle(3, True).Y + DataGridView1.Columns(3).HeaderCell.ContentBounds.Height + 8), DataGridView1.GetColumnDisplayRectangle(3, True).Width - 1, (DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Top - DataGridView1.GetRowDisplayRectangle((DataGridView1.Rows.Count - 1), True).Height))
    e.Graphics.FillRectangle(Brushes.White, rct1)
    e.Graphics.DrawString(
    "VERTICAL" & vbCrLf & "TEXT 3rd MERGED COLUMN", fnt, Brushes.Black, rct1)
    e.Graphics.FillRectangle(Brushes.White, rct2)
    e.Graphics.DrawString(
    "VERTICAL" & vbCrLf & "TEXT 4th MERGED COLUMN", fnt, Brushes.Black, rct2)
    Dim rct As Rectangle = DataGridView1.GetRowDisplayRectangle(3, True)
    rct.Height -= 1
    Dim s As SizeF = e.Graphics.MeasureString("HORINZONTAL TEXT", DataGridView1.Font)
    Dim lefts As Integer = (rct.Width / 2) - (s.Width / 2)
    Dim tops As Integer = rct.Top + ((rct.Height / 2) - (s.Height / 2))
    e.Graphics.FillRectangle(Brushes.White, rct)
    e.Graphics.DrawString(
    "HORINZONTAL TEXT", fnt, Brushes.Black, 2, tops)

    Again, as I said before, you need to write really too many lines to get custom control. These are only small parts of codes that can only give you picture how should it look like. Any further coding you need to carry on your own... Anyways, hope this was helpful for both of you...

    Happy coding

    Friday, February 02, 2007 7:52 AM
  • Hi Sir

     

        Thanks for replying.

               I was writing a  following code from your help for merging the two cells in datagridview. But  when i am changing the size of column of merged cell the cells next to this cells are not chaging their positions according to this merged cells. so plz see this code and help me. It's Urgent. Give reply as soon as possible.

     

    Dim rct As Rectangle

    rct.X = dgv1.GetColumnDisplayRectangle(2, True).X

    rct.Y = dgv1.GetRowDisplayRectangle(1, True).Y

    rct.Height = dgv1.Rows(1).Cells(0).Size.Height

    rct.Width = dgv1.Rows(1).Cells(1).Size.Width + dgv1.Rows(1).Cells(2).Size.Width

    e.Graphics.FillRectangle(Brushes.White, rct)

    e.Graphics.DrawRectangle(Pens.Blue, rct)

     

     

    Sunday, February 04, 2007 4:30 AM
  • Hi ismar

           I have a problem to dispalying the image on Datagridview Column Header. I am writing the code on datagridview paint event. when scrolling the horizontal scroll bar then image dispaly on the another column header. it is not working properly. so please help me how to solve this problem.

    Dim rect As New rectangle

    Dim img As Image

    img = Image.FromFile("c:\ws9.png")

    rct.X = dgv1.GetC olumnDisplayRectangle(0, True).X

    rct.Y = 0

    rct.Height = dgv1.Columns(0).HeaderCell.Size.Height

    rct.Width = dgv1.Columns(0).HeaderCell.Size.Width

    e.Graphics.DrawImage(img, rct)

     

     

     

    Monday, February 05, 2007 10:17 AM
  • Can we do this at run time?

     

    Means when i select particular area it will be merged.

     

    Is it possible?

     

    Thanks in advance

     

    Friday, January 18, 2008 11:56 AM
  • This is probably considered a hack but it works for me.

     

    I have 7 columns, I want to merge cells 1 to 6 (don't forget zero based)

     

    When the grid is rendered it is rendered as html so just intercept it in the RowDataBound Event, then merge the cells as you would in html.  Close out the open td tag.  Insert your new tag with the colspan attribute.  then close your td.

    for display purposes Hide the columns you are merging over.

     

    e.Row.Cells(1).Text = "</td><td colspan=5> " & e.Row.Cells(1).Text & "</td>"

    e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center

    e.Row.Cells(2).Visible = False

    e.Row.Cells(3).Visible = False

    e.Row.Cells(4).Visible = False

    e.Row.Cells(5).Visible = False

    e.Row.Cells(6).Visible = False

    Thursday, March 06, 2008 12:35 AM
  •  ismar wrote:

    Hi all,

    Is it possible to merge cells(rows) in DatagridView And How?

    Thanx

    Monday, July 14, 2008 4:51 AM
  • Can U up your dll file or solution?
    Tuesday, July 29, 2008 11:48 AM
  • Hey, I am having the same problem.  I've painted a 16x16 icon and every column header using the dataGridView onPaint event.  When the user scrolls horizontally, the leftmost column header icon image overlaps onto the row header column.  I would like to either check to not paint that icon image on the left most column header or I could programmically scroll the horizontal scoll bar so that the left most column is fully displayed.  I don't know how to do either of these two options.  Were you able to get an answer or solve your problem?
    Friday, August 15, 2008 4:41 PM
  • Hi KimmyGoose

    I suppose that you cannot hide the row headers of your datagridview

    Instead of trying to paint an icon in the OnPaint event of the DataGridView use OnCellPainting event

    In that check for rowindex == -1 and columnindex == 'your column index' (For a row header, columnindex = -1)
    Under this if condition you can write your code to draw the icon image
    Tuesday, December 30, 2008 8:19 AM
  • It is very interesting the way you worked it out, I just have one doubt, how do you exactly inctercept the rowdatabound event?, we are talking about the datagridview and not asp's gridview aren't we ?
    Saturday, February 14, 2009 1:48 AM
  • Thanks Heberts_tkd

    one question after so long time, are you talking about Datagridview in Web forms or Windows forms.
    please reply soon

    Tejas Kishanwala
    • Proposed as answer by etsat Tuesday, August 25, 2009 8:57 AM
    Friday, August 07, 2009 8:34 AM
  • Hi folks!

    I've been trying to figure out lately the shortest way to arrange a grid that allows cell merge. I really don't know why this is not a default feature in DatagridView.

    Tejas, the DataGridView control is used in Windows Forms. You only have access to this control when starting a new Windows Forms Project. For web grid controls there are other controls named differently that show up on web based Visual Studio projects.

    Finally I've recently found the following free to use and distribute customizable grid:

    http://sourcegrid.codeplex.com/

    I'm starting now to test its features but it seems pretty much an OK project. Hope this helps.


    Regards,

    Sérgio Teixeira
    Tuesday, August 25, 2009 9:19 AM