C# .NET - Winforms: How to Group columns in DataGridView

Asked By Marco Gomez on 18-Oct-11 01:34 AM
Hi!

I know that there are available 3rd party packages that extends the .net dataGridView to handle grouping by columns,, but is there a way to implement it using the native .net dataGridView? I would want to drag a column to the top of the table and group the columns by that dragged column.

I am currently experimenting on this. I hope you guys have suggestions.

Thanks!
smr replied to Marco Gomez on 18-Oct-11 01:39 AM
hi

try this

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


follow  http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq8
Web Star replied to Marco Gomez on 18-Oct-11 01:39 AM

The built-in WinForms DGV does not support grouping. Here is an article where someone has added this functionality themselves (note that I have not used this myself and so am not making a recommendation):http://blogs.vbcity.com/hotdog/archive/2008/12/19/9226.aspx

Alternately there are countless third-party DGV controls for sale. Infragistics has an excellent one that I can recommend which does provide grouping:http://www.infragistics.com/dotnet/netadvantage/winforms/wingrid.aspx#Overview

Anoop S replied to Marco Gomez on 18-Oct-11 02:15 AM
Well there are third party components that support what you want to do. Not sure whether any of Microsoft's .NET components will work for you. You can certainly generate "live" reports with Crystal reports. I can't say whether you can add context menu's to them however. I wouldn't be surprised though if they have that kind of feature, it's just not likely to be included in the free version you get with your development tools.

What you could do is break it up into multiple queries (by category) and then interleave the results of those with your own headings into a list box. Or just have multiple datagrids on the the screen, each with it's own heading.