In this tutorial, we are going to see how we can merge DataGridView Cells in C# Windows Application. DataGridView is always an important control in Windows Application for showing Table Data. In some cases, we need to merge two or more cells in DataGridView for showing data as a Report. Here we are going to develop the same example in C# Windows Application.

After Completing this tutorial you will be able to understand:

  • How to Merge DataGridView Cells in C# Windows Application.

How to Merge DataGridView Cells in C# Windows Application:

Step 1: Open Visual Studio and Create a C# Windows Application called MergeDataGrdViewCellsDemo.

Merge DataGridView Cells

Step 2: Drag a DataGridView on the Windows Form.

Step 3: Set the Name property of the DataGridView to dgvData.

Step 4: Set the Dock Property of DataGridView to Fill.

Step 5: Set the AutoSizeColumnMode of DataGridView to Fill.

Your Windows Form should look like this.

Merge DataGridView Cells

Step 6: The Next step is to prepare DataTable for DataGridView. Open Code Behind File and write the following method.

public DataTable CreateDataTable()
{
DataTable dtData = new System.Data.DataTable();
DataColumn dcID = new System.Data.DataColumn("ID");
DataColumn dcName = new System.Data.DataColumn("Name");

DataColumn dcAge = new System.Data.DataColumn("Age");
dtData.Columns.Add(dcID);
dtData.Columns.Add(dcName);

dtData.Columns.Add(dcAge);
return dtData;
}

Step 7: Now we need to set Data to the DataTable. Write the following method in Code Behind File.


public DataTable SetData()
 {
 DataTable dtData = CreateDataTable() ;
 DataRow drData = dtData.NewRow();
 drData["ID"] = 1;
 drData["Name"] = "Arun Singh";
 drData["Age"] = 20;
 dtData.Rows.Add(drData);
 drData = dtData.NewRow();
 drData["ID"] = 2;
 drData["Name"] = "Vikas Singh";
 drData["Age"] = 22;
 dtData.Rows.Add(drData);

drData = dtData.NewRow();
 drData["ID"] = 3;
 drData["Name"] = "Ketan Pandey";
 drData["Age"] = 22;
 dtData.Rows.Add(drData);

drData = dtData.NewRow();
 drData["ID"] = 4;
 drData["Name"] = "Ravi Singh";
 drData["Age"] = 27;
 dtData.Rows.Add(drData);

drData = dtData.NewRow();
 drData["ID"] = 5;
 drData["Name"] = "Ashwin Roy";
 drData["Age"] = 26;
 dtData.Rows.Add(drData);

drData = dtData.NewRow();
 drData["ID"] = 6;
 drData["Name"] = "Chetan Pandey";
 drData["Age"] = 26;
 dtData.Rows.Add(drData);

 return dtData;
 }

Step 8: Write the following code on Form Load event.


 dgvData.DataSource = SetData();
 dgvData.AutoGenerateColumns = false;

Step 9: Create a method to check the value of cells are equal or not.


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


Step 10: Write the following code on DataGridView Cell Painting Event.


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 = dgvData.AdvancedCellBorderStyle.Top;
 }

Step 11: Now finally write the following code on DataGridView Cell Formatting Event.


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

Now run the application and look over the output window.

Merge DataGridView Cells

Here is the Complete Source Code for the application.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
namespace MergeDataGrdViewCellsDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
dgvData.DataSource = SetData();
dgvData.AutoGenerateColumns = false;

}
public DataTable CreateDataTable()
{
DataTable dtData = new System.Data.DataTable();
DataColumn dcID = new System.Data.DataColumn("ID");
DataColumn dcName = new System.Data.DataColumn("Name");

DataColumn dcAge = new System.Data.DataColumn("Age");
dtData.Columns.Add(dcID);
dtData.Columns.Add(dcName);

dtData.Columns.Add(dcAge);
return dtData;
}
public DataTable SetData()
{
DataTable dtData = CreateDataTable() ;
DataRow drData = dtData.NewRow();
drData["ID"] = 1;
drData["Name"] = "Arun Singh";
drData["Age"] = 20;
dtData.Rows.Add(drData);
drData = dtData.NewRow();
drData["ID"] = 2;
drData["Name"] = "Vikas Singh";
drData["Age"] = 22;
dtData.Rows.Add(drData);

drData = dtData.NewRow();
drData["ID"] = 3;
drData["Name"] = "Ketan Pandey";
drData["Age"] = 22;
dtData.Rows.Add(drData);

drData = dtData.NewRow();
drData["ID"] = 4;
drData["Name"] = "Ravi Singh";
drData["Age"] = 27;
dtData.Rows.Add(drData);

drData = dtData.NewRow();
drData["ID"] = 5;
drData["Name"] = "Ashwin Roy";
drData["Age"] = 26;
dtData.Rows.Add(drData);

drData = dtData.NewRow();
drData["ID"] = 6;
drData["Name"] = "Chetan Pandey";
drData["Age"] = 26;
dtData.Rows.Add(drData);
return dtData;
}
public bool IsTheSameCellValue(int column, int row)
{
DataGridViewCell cell1 = dgvData[column, row];
DataGridViewCell cell2 = dgvData[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}

private void dgvData_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 = dgvData.AdvancedCellBorderStyle.Top;
}
}

private void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}
}
}

View More:

Conclusion:

Hope you understand this post about How to Merge DataGridView Cells in C# Windows Application. I will appreciate your feedback, comments and suggestions.

Thank You.

Leave a Comment