2013-05-27 25 views
12

我在當前顯示像這樣的網格一些數據:如何合併的DataGridView單元格中的WinForms

------------------ 
|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  | 
------------------ 

有什麼辦法我可以合併這些單元格? 我也嘗試過很多方面也谷歌,但沒有找到任何合適的方式。 如果可以用另一種方式顯示這些數據而不使用datagridview,但結果是我已經顯示的方式,那也可以解決我的問題。

回答

5

DataGridView控件沒有相關的屬性或方法來合併單元格,但您可以使用自定義繪畫完成相同的操作。您可以使用DataGridView.CellPainting事件或覆蓋Paint方法。

此外,您還需要重寫DataGridView.CellClick,CellEnter,CellFormatting和其他方法以便爲您的DataGridView提供全功能功能。例如,在單元格單擊時,整個合併單元格(或構成合並單元格的單元格組)將不得不自定義繪製。

您可以在這裏找到一些示例代碼:

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

25

你必須先找到一個重複的值

需要兩種方法:

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; 

Image of DGV_Merge

+0

出色的工作。 – user2211290

+1

幹得好!你能幫我怎麼做列合併,我想單個元素跨越多個列 – robot

0

在asp.net上有一些很好的回覆,但是在winforms和這個例子中(在列中合併相同的數據)它沒有被定義。

您可以使用color.transparent在datagridview中隱藏相同的值。即使通過此代碼,相同的行也不會被刪除,並且可以用於數學計算。即使您可以根據您的需求來定義要合併的列,也可以使用 。

以這種方式,如果「A」的結尾是「A4」並且「B」的開始是「A4」,則這些不會合並。這通常是更期望的。(如果你不想這樣,最好使用其他響應)

MergeGridviewCells(DGV,new int [] {0,1}); //例如,如果你想合併第一列數據,然後第三列,然後第二欄就可以使用新的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; 
      } 
     } 
    } 

enter image description here

相關問題