Home > Backend Development > C++ > How to Merge Duplicate Cells in a WinForms DataGridView's First Column?

How to Merge Duplicate Cells in a WinForms DataGridView's First Column?

Mary-Kate Olsen
Release: 2025-01-12 15:21:47
Original
328 people have browsed it

How to Merge Duplicate Cells in a WinForms DataGridView's First Column?

WinForms DataGridView Cell Merge

Question:

In DataGridView, data is arranged in a grid format, containing rows and columns. Each row and column intersection is represented by a cell. In this example, the data is displayed in a format such that duplicate values ​​in the first column (Hd1) are not merged, resulting in multiple rows for the same value.

Requirements:

The goal is to merge cells with duplicate values ​​in the first column (Hd1) to improve visuals and consolidate data efficiently.

Solution:

To merge cells in DataGridView, you can follow these steps:

  1. Find duplicate values:

    • Create a method named IsTheSameCellValue that compares the values ​​of two adjacent cells in the first column. If the values ​​are the same, true is returned, otherwise false is returned.
<code class="language-csharp">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();
}</code>
Copy after login
  1. Cell drawing:

    • Override the CellPainting event handler and modify the cell's AdvancedBorderStyle property:
<code class="language-csharp">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;
    }  
}</code>
Copy after login
  1. Cell formatting:

    • Override the CellFormatting event handler to set the value to an empty string if the cell is a duplicate:
<code class="language-csharp">if (e.RowIndex == 0)
    return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
    e.Value = "";
    e.FormattingApplied = true;
}</code>
Copy after login
  1. Other settings:

    • Set dataGridView1.AutoGenerateColumns to false in the form load event to prevent columns from being automatically generated.
<code class="language-csharp">dataGridView1.AutoGenerateColumns = false;</code>
Copy after login

Result:

Performing these steps will merge cells with duplicate values ​​in the first column of the DataGridView, making the data display more compact and organized.

The above is the detailed content of How to Merge Duplicate Cells in a WinForms DataGridView's First Column?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template