C# 如何在 Winforms 中合并 DataGridView 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16774966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Merge DataGridView Cell in Winforms
提问by M. Rain
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.
有什么办法可以合并这些单元格吗?我也尝试了很多方法,但没有找到任何合适的方法。如果可以在不使用 datagridview 的情况下以另一种方式显示此数据,但结果是我显示的方式,那也将解决我的问题。
回答by Aseem Gautam
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.CellPaintingevent or override the Paint method.
DataGridView 控件没有用于合并单元格的相关属性或方法,但您可以使用自定义绘制来完成相同的操作。您可以使用DataGridView.CellPainting事件或覆盖 Paint 方法。
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.
此外,您还需要覆盖 DataGridView.CellClick、CellEnter、CellFormatting 和其他方法,以便为您的 DataGridView 提供完整的功能。例如,在单击单元格时,必须自定义绘制整个合并单元格(或构成合并单元格的单元格组)。
You can find some sample code here:
你可以在这里找到一些示例代码:
http://social.msdn.microsoft.com/forums/en-US/vbinterop/thread/5b659cbd-7d29-4da4-8b38-5d427c3762e2
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://forums.codeguru.com/showthread.php?415930-DataGridView-Merging-Cells
http://www.codeproject.com/Questions/152113/How-can-i-merge-DataGridView-Rows-Cells-with-Equal
http://www.codeproject.com/Questions/152113/How-can-i-merge-DataGridView-Rows-Cells-with-Equal
回答by ghasem deh
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:
并在 form_load 中:
dataGridView1.AutoGenerateColumns = false;


回答by Ehsan R
There are some good responses on asp.net but in winforms and for this example(merging same data in columns) it is not defined.
在 asp.net 上有一些很好的反应,但在 winforms 中,对于这个例子(在列中合并相同的数据)它没有定义。
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.
您可以使用 color.transparent 在 datagridview 中隐藏相同的值。即使通过这段代码,相同的行也不会被删除,并且有利于数学计算。甚至您可以根据您的愿望排序定义要合并的列。
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)
这样,如果“A”的结尾是“A4”,而“B”的开头也是“A4”,则不会合并。这通常是更需要的。(如果您不想要这个,最好使用其他响应)
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}
MergeGridviewCells(DGV,new int[] {0,1});//例如如果你想合并第一列数据/然后第三列然后第二列你可以使用 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:
预览:

