在DataGridView中对行进行分组

时间:2014-06-30 09:25:41

标签: c# winforms datagridview

我想在Windows窗体上的DataGridView中对具有相同名称的行进行分组,这是我想要实现的图像。

是否可以在不使用任何第三方工具的情况下实施以下内容?

sample

3 个答案:

答案 0 :(得分:5)

DataGridView中的

将以下代码放在

dgvProduct_CellFormatting Event

If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                If dgvProduct.Item(0, e.RowIndex - 1).Value = e.Value Then
                    e.Value = ""
                ElseIf e.RowIndex < dgvProduct.Rows.Count - 1 Then
                    dgvProduct.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                End If
End If

全部完成!

享受

enter image description here

答案 1 :(得分:3)

您可以尝试使用垂直单元格合并的MSFlexGrid MergeCells属性的功能,而不是本文DataGridView Grouping in C#/VB.NET: Two Recipes中所述的行分组。在此示例中,属于组的行使用垂直合并的单元格可视地连接 - 而不是使用传统的水平组行。

enter image description here

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs args)
{
  base.OnCellPainting(args);

  args.AdvancedBorderStyle.Bottom =
    DataGridViewAdvancedCellBorderStyle.None;

  // Ignore column and row headers and first row
  if (args.RowIndex < 1 || args.ColumnIndex < 0)
    return;

  if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
  {
    args.AdvancedBorderStyle.Top =
      DataGridViewAdvancedCellBorderStyle.None;
  }
  else
  {
    args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top;
  }
}

答案 2 :(得分:1)

补充(选择)答案,这是完整的代码。未提及的想法是扩展DataGridView类的类。

public class GroupByGrid : DataGridView
    {

        protected override void OnCellFormatting(
           DataGridViewCellFormattingEventArgs args)
        {
            // Call home to base
            base.OnCellFormatting(args);

            // First row always displays
            if (args.RowIndex == 0)
                return;


            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.Value = string.Empty;
                args.FormattingApplied = true;
            }
        }

        private bool IsRepeatedCellValue(int rowIndex, int colIndex)
        {
            DataGridViewCell currCell =
               Rows[rowIndex].Cells[colIndex];
            DataGridViewCell prevCell =
               Rows[rowIndex - 1].Cells[colIndex];

            if ((currCell.Value == prevCell.Value) ||
               (currCell.Value != null && prevCell.Value != null &&
               currCell.Value.ToString() == prevCell.Value.ToString()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected override void OnCellPainting(
           DataGridViewCellPaintingEventArgs args)
        {
            base.OnCellPainting(args);

            args.AdvancedBorderStyle.Bottom =
               DataGridViewAdvancedCellBorderStyle.None;

            // Ignore column and row headers and first row
            if (args.RowIndex < 1 || args.ColumnIndex < 0)
                return;

            if (IsRepeatedCellValue(args.RowIndex, args.ColumnIndex))
            {
                args.AdvancedBorderStyle.Top =
                   DataGridViewAdvancedCellBorderStyle.None;
            }
            else
            {
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top;
            }
        }
    }

source由social.msdn.microsoft提供