DataGridView で行をグループ化する

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

すべて完了!

楽しむ


この記事「C#/VB.NET での DataGridView グループ化:2 つのレシピ」で説明されているように、行グループ化の代わりに垂直セル結合の MSFlexGrid の MergeCells プロパティの機能を使用してみることができます。この例では、従来の水平方向のグループ行を使用する代わりに、垂直方向に結合されたセルを使用して、グループに属する行が視覚的に結合されています。

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