Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group rows in DataGridView

I want to group rows which is having same Name in DataGridView on Windows Forms below is the image what I want to implement.

Is it possible to implement below without using any third party tool ?

sample

like image 360
Deeps Avatar asked Jun 30 '14 09:06

Deeps


3 Answers

in the DataGridView place the following code in the

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

All done!

Enjoy

enter image description here

like image 136
Mou Avatar answered Nov 08 '22 12:11

Mou


You could try using the functionality of MSFlexGrid's MergeCells property of vertical cell merging instead of row grouping as explained in this article DataGridView Grouping in C#/VB.NET: Two Recipes. In this example, rows which belong to a group are joined visually using cells merged vertically - instead of using classical horizontal group rows.

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;
  }
}
like image 39
chridam Avatar answered Nov 08 '22 10:11

chridam


To supplement the (chosen) answer, here's the full code. The unmentioned idea is a class extending the DataGridView class.

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 courtesy of social.msdn.microsoft

like image 27
JavaHead1560 Avatar answered Nov 08 '22 11:11

JavaHead1560