Group rows in a DataGridView

I want to group rows that have the same name in the DataGridView in Windows Forms below - this is the image I want to implement.

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

sample

+6
source share
3 answers

You can try using the MSFlexGrid MergeCells property functionality to merge cells vertically instead of grouping rows, as described in this DataGridView Grouping article in C # / VB.NET: Two recipes . In this example, rows belonging to a group are connected visually using cells joined vertically instead of using classic rows of a horizontal group.

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; } } 
+3
source

in the DataGridView put the following code in

 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 

Done!

Enjoy

enter image description here

+4
source

To complement the (selected) answer, here is the full code. An undefined idea is a class that extends 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 provided by social.msdn.microsoft

+1
source

Source: https://habr.com/ru/post/971528/


All Articles