如何在WinForms中合并DataGridView单元格

我有一个网格中的数据,目前显示如下:

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

我想使它看起来像这样:

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

有什么方法可以合并这些单元格吗? 我也尝试了很多方式谷歌,但没有find任何合适的方式。 如果可以用另一种方式显示这些数据,而不使用datagridview,但结果是我已经显示,这也将解决我的问题。

你必须先find重复的值

需要两种方法:

 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(); } 

在这种情况下,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; } } 

现在在单元格格式中:

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

并在form_load:

 dataGridView1.AutoGenerateColumns = false; 

DGV_Merge的图片

DataGridView控件没有相关的属性或方法来合并单元格,但是您可以使用自定义绘画完成相同的操作。 您可以使用DataGridView.CellPainting事件或重写Paint方法。

另外,您还需要重写DataGridView.CellClick,CellEnter,CellFormatting和其他方法,以便为您的DataGridView提供全function的function。 例如,在单元格单击时,整个合并单元格(或组成合并单元格的单元格组)将不得不自定义绘制。

你可以在这里find一些示例代码:

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

在asp.net上有一些好的反应,但在winforms和这个例子(合并列中相同的数据),它没有定义。

你可以使用color.transparent在datagridview中隐藏相同的值。 即使通过这个代码,相同的行不会被删除,并且可以用于math计算。 即使你可以根据你的需求来定义合并的列。

这样,如果“A”的结尾是“A4”并且“B”的开始是“A4”,则这些不会合并。 这通常是更期望的。 (如果你不想要这个,最好使用其他答案)

(例如,如果你想合并第一列的数据/然后第三列,然后第二列,你可以使用新的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; } } } 

在这里input图像说明