Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a datagridview that's already bound to a database. For each row in my datagridview, it shows a minimal, avg/nominal and maximum value. However, I'd like it if there is only one value that I give it, to merge cells into one cell, just like in the picture below :


https://i.stack.imgur.com/CVzWg.png

What I have tried:

I managed so far to merge cells in the same column with the same value.

It does work for the first column but not the 3rd one, and I don't get why

if (e.ColumnIndex == 1)
            {
                
                e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
                if(e.RowIndex == 0) {
                    e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single;

                }
if (e.RowIndex < 1 || e.ColumnIndex < 0)
  return;
    if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
        {
           
       e.AdvancedBorderStyle.Top=DataGridViewAdvancedCellBorderStyle.None;
                    
                    }
    else
       {
      e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single;
                    }
            }
      if ((e.ColumnIndex == 3 ))
            {
                if(e.Value is null)
                {
                    e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
                }
            }
Posted
Updated 21-Jun-23 23:01pm
v3

Google search is your friend: winform datagridview merge cells - Google Search[^]
First search result: How to Merge DataGridView Cell in Winforms - StackOverflow[^]

The default control is limited in what it can do. You will need to use a 3rd-party control to do advanced formatting, like merged column cells.

Here is one: WinForms Grid Control | .NET Grid for Windows Forms | Syncfusion[^]. They have a "Community License provides free access to all the Syncfusion products for individual developers and small businesses." more info here: 1,800+ Free controls and frameworks for desktop, web, and mobile apps.[^]
 
Share this answer
 
v3
Comments
LiterallyGutsFromBerserk 21-Jun-23 7:07am    
I have managed to do this so far : https://i.stack.imgur.com/rl8Po.png using this exact same post XD.

I'm also going to export the form to Excel anyways. Should I do the merging when I export, or should I stick with the third party?
This is the same question you pasted this morning: Change a single cell's border style in datagridview[^]
 
Share this answer
 
Comments
LiterallyGutsFromBerserk 21-Jun-23 7:22am    
No, this morning's question is for vertical merging.Now I'm asking for horizontal merging, seeing as I was told it was harder or impossible.
Dave Kreskowiak 21-Jun-23 10:27am    
No difference at all. The standard grid in the Framework doesn't support merging at all, in any direction.

Either use 3rd party controls that do support merging, or write your own version of the control to support what you want.
LiterallyGutsFromBerserk 21-Jun-23 10:45am    
I updated the question, seeing as the problem evolved.
Dave Kreskowiak 21-Jun-23 11:24am    
You're not "merging cells." You're faking it by drawing borders to give the illusion of "merged cells."
LiterallyGutsFromBerserk 21-Jun-23 11:28am    
Oh yeah, I'm only overriding the paint method. But it doesn't work for some columns. Any idea why?
I managed to find a solution with a PaintCell override :

C#
private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                
                e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
                if(e.RowIndex == 0) {
                    e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single;

                }
                if (e.RowIndex < 1 || e.ColumnIndex < 0)
                    return;
                if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
                  {
                        e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
                    
                  }
                else
                  {
                        e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.Single;
                  }
            }
            else 
            {
                if ((e.Value?.ToString() == "") && (e.ColumnIndex == 3))
                {
                    e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
                }



                 if ((e.ColumnIndex == 4))
                {
                   
                    if((e.RowIndex>=0)&&(dataGridView2[5,e.RowIndex].Value?.ToString()==""))
                    {
                        e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None;
                    }
                }
                    
                }
            }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900