【问题标题】:How to Merge DataGridView Cell in Winforms如何在 Winforms 中合并 DataGridView 单元格
【发布时间】:2013-05-22 09:58:54
【问题描述】:

我在网格中有一些数据,当前显示如下:

------------------
|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 的情况下以另一种方式显示这些数据,但结果是我展示的方式,那也将解决我的问题。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    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

    【讨论】:

      【解决方案2】:

      你必须先找到重复的值

      需要两种方法:

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

      在事件中,细胞绘画:

      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;
      

      【讨论】:

      • 干得好!你能帮我如何进行列合并,我希望单个元素跨多个列
      • 如果合并单元格文本对齐必须居中,那么这种方法行不通?合并两个单元格的中间中心。
      【解决方案3】:

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

      您可以使用 color.transparent 在 datagridview 中隐藏相同的值。 即使通过此代码,也不会删除相同的行,并且可以很好地进行数学计算。 甚至您也可以根据需要定义要合并的列。

      这样,如果“A”的结尾是“A4”并且“B”的开头也是“A4”,则不会合并。这通常是更需要的。 (如果您不想要这个,最好使用其他响应)

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

      预览:

      【讨论】:

        猜你喜欢
        • 2019-02-25
        • 2012-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        相关资源
        最近更新 更多