Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wants to implement cell merging of datagridview. I found one class file which contain the code but i dont know how to use / call these function.
following events in class file on datagridview of windows form:-

C#
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;
                }
            }
        }
Posted
Comments
Ralf Meier 11-Jun-15 6:52am    
What do you want to do exactly ?
Which is your criterion for merging ?
ranjeet11 11-Jun-15 6:56am    
wants to assign above events in class file to datagridview
for merging duplicate record in a cell is criteria
Sinisa Hajnal 11-Jun-15 6:57am    
Looks like you don't have to do anything.

Use this controls instead of regular DataGridView and the cells will be merged if they have same values.

I would add property to be able to control this behaviour, but as-is it will work.
ranjeet11 11-Jun-15 7:02am    
i found this solution on
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq8
.they not mention its third party control
Sinisa Hajnal 11-Jun-15 10:06am    
Look at hte top of the class. It states it inherits DataGridView overriding the events needed to get the functionality you need as it is not supported by default grid.

It is not third party control per se, it is just that someone on the forum wrote what he needed. Just as you will have to if you want this functionality.

If you don't want to use this snippet, you can consider DevExpress third party (or any other) containing grid control with fully controlable grouping...but it is a commercial package.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900