24

I have some data in a grid that currently displays like this:

------------------
|Hd1| Value  |
------------------
|A  | A1     |
------------------
|A  | A2     |
------------------
|A  | A3     |
------------------
|A  | A4     |
------------------
|B  | B1     |
------------------
|B  | B2     |
------------------
|B  | B3     |
------------------
|B  | B4     |
------------------
|B  | B5     |
------------------
|C  | C1     |
------------------
|C  | C2     |
------------------

I want to make it look like this:

|Hd | Value  |
------------------
|A  | A1     |
    ----------
|   | A2     |
    ----------
|   | A3     |
    ----------
|   | A4     |
------------------
|B  | B1     |
    ----------
|   | B2     |
    ----------
|   | B3     |
    ----------
|   | B4     |
    ----------
|   | B5     |
------------------
|C  | C1     |
    ----------
|   | C2     |
------------------

Is there any way that I can merge these cells? I have tried in many ways also google but did not find any suitable way. If it is possible showing this data another way without using datagridview but the result is the way I have showed, that will also solve my problem.

4 Answers 4

43

You must first find a duplicate values

Need to two methods:

bool IsTheSameCellValue(int column, int row)
{
    DataGridViewCell cell1 = dataGridView1[column, row];
    DataGridViewCell cell2 = dataGridView1[column, row - 1];
    if (cell1.Value == null || cell2.Value == null)
    {
       return false;
    }
    return cell1.Value.ToString() == cell2.Value.ToString();
}

in the event, cellpainting:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
    if (e.RowIndex < 1 || e.ColumnIndex < 0)
        return;
    if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
    {
        e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
    }
    else
    {
        e.AdvancedBorderStyle.Top = dataGridView1.AdvancedCellBorderStyle.Top;
    }  
}

now in cell formatting:

if (e.RowIndex == 0)
    return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
    e.Value = "";
    e.FormattingApplied = true;
}

and in form_load:

dataGridView1.AutoGenerateColumns = false;

Image of DGV_Merge

3
  • 1
    Good work! can you help me how i can do column merging, i want single element span across multiple columns
    – Rakesh
    May 24, 2017 at 12:24
  • If merge cells text alignment has to be middle center then this approach will not work? Middle center of merge two cell.
    – Darshana
    Nov 12, 2021 at 0:54
  • Sorry I am very new to C# and winForm, where should the cell formatting part of code be written? Inside of the .designer file or is there a event for cell formatting that I should create? Any tip would be super helpful, thank you in advance.
    – Emory Lu
    May 23, 2022 at 14:01
5

The DataGridView control has no related properties or methods to merge cells, but you can accomplish the same using custom painting. You can use DataGridView.CellPainting event or override the Paint method.

Plus you will need to override the DataGridView.CellClick, CellEnter, CellFormatting and other methods as well in order to give your DataGridView a full featured functionality. For eg on cell click, the entire merged cell (or group of cells that constitute a merged cell) will have to be custom painted.

You can find some sample code here:

http://social.msdn.microsoft.com/forums/en-US/vbinterop/thread/5b659cbd-7d29-4da4-8b38-5d427c3762e2

http://forums.codeguru.com/showthread.php?415930-DataGridView-Merging-Cells

http://www.codeproject.com/Questions/152113/How-can-i-merge-DataGridView-Rows-Cells-with-Equal

2

I spent a long time looking for this as my boss didn't want to buy any off-the-shelf components. This should be submitted into the .NET code: datagridvewtextboxcell-with-span-behaviour It just works and is soo simple to use. Works with VB/C# .NET 4.5 to 6. Spans rows and columns including headers.

DataGridView.Columns.Add(new DataGridViewTextBoxColumnEx());
DataGridViewTextBoxCellEx dataGridViewCell = (DataGridViewTextBoxCellEx)DataGridView[colIdx, rowIdx];
dataGridViewCell.ColSpan = 2;
dataGridViewCell.RowSpan = 6;
1

There are some good responses on asp.net but in winforms and for this example(merging same data in columns) it is not defined.

You can use color.transparent to hide the same values in datagridview. even by this code, same rows do not deleted and can be good for matematical calculations. even you can define which columns to merge by your desire sort.

In this way if end of "A" was "A4" and also start of "B" was "A4" those would not be merge. which is Often more desired. (If you do not want this, better use other responses)

MergeGridviewCells(DGV,new int[] {0,1});//For example if you want to merge first columns data/then 3th column and then second column you can use new int[] {0,2,1}

private void MergeGridviewCells(DataGridView DGV, int[] idx)
    {
        DataGridViewRow Prev = null;

        foreach (DataGridViewRow item in DGV.Rows)
        {
            if (Prev != null)
            {
                string firstCellText = string.Empty;
                string secondCellText = string.Empty;

                foreach (int i in idx)
                {                        
                    DataGridViewCell firstCell = Prev.Cells[i];
                    DataGridViewCell secondCell = item.Cells[i];

                    firstCellText = (firstCell != null && firstCell.Value != null ? firstCell.Value.ToString() : string.Empty);
                    secondCellText = (secondCell != null && secondCell.Value != null ? secondCell.Value.ToString() : string.Empty);

                    if (firstCellText == secondCellText)
                    {                           
                        secondCell.Style.ForeColor = Color.Transparent;
                    }
                    else
                    {
                        Prev = item;
                        break;
                    }
                }
            }
            else
            {
                Prev = item;
            }
        }
     }

Preview:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.