C# Winform: How to develop merge column with Datagridview.

T.Zacks 3,996 Reputation points
2023-02-22T13:38:32.3+00:00

I am looking for assitance that how to merge few specific columns with Datagridview. i am attaching a picture.

User's image

in the picture row which has Eric World that is data row from where data will be there in grid.

please provide a sample code to design a grid with merge columns with datagridview.

Thanks

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-02-24T08:36:41.99+00:00

    Hi,

    DataGridView is difficult to achieve what you want.
    There are some examples of using custom GridView on Github for your reference.

    https://github.com/wsn931203/RowMergeView
    User's image

    Form1_Load:  
                this.rowMergeView1.Columns.Add("1", "Analyst");
                this.rowMergeView1.Columns.Add("2", "Date");
                this.rowMergeView1.Columns.Add("3", "Current");
                this.rowMergeView1.Columns.Add("4", "Previous");
                this.rowMergeView1.Columns.Add("5", "Current");
                this.rowMergeView1.Columns.Add("6", "Previous");
                this.rowMergeView1.Columns.Add("7", "Future");
                this.rowMergeView1.Rows.Add("1", "2", "3", "4", "5", "6", "7");
                this.rowMergeView1.ColumnHeadersHeight = 40;
                this.rowMergeView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
                
                this.rowMergeView1.AddSpanHeader(2, 2, "Price Target");
                this.rowMergeView1.AddSpanHeader(4, 3, "Rating");
    
                for (int i = 0; i < this.rowMergeView1.Columns.Count; i++)
                {
                    this.rowMergeView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
    
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.