Click here to Skip to main content
16,015,991 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi

i have a gridview , i want to merge cells with a same value in one column

i have 8 columns , but i want to merge cells on column number one

how can i do this

thnx in advance
Posted
Updated 10-Jul-15 2:12am
Comments
Thanks7872 10-Jul-15 8:15am    
What have you tried?
sns60 10-Jul-15 10:12am    
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];

for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)
{
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
}


i used it

but it merge all cells of all columns
i want to merge same cells in one column
Sergey Alexandrovich Kryukov 10-Jul-15 13:48pm    
GridView? Which one? Why do you think there is only one? Full type name, please.
—SA

1 solution

There are a lot of good examples. For example:
- How to merge cells with equal values in a GridView[^]
- Rows and Columns Merging in ASP.NET GridView Control[^]
and so on...

ADDITION:
merge just 1st column (based on the code in first article)
C#
public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            if (row.Cells[0].Text == previousRow.Cells[0].Text)
            {
                row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 : 
                                       previousRow.Cells[0].RowSpan + 1;
                previousRow.Cells[0].Visible = false;
            }
        }
    }
 
Share this answer
 
v2
Comments
sns60 10-Jul-15 10:10am    
thanx

i have done this before
but
these examples merged all cells in all column in my grid

i want to merge same cells in one column in grid

thnX for helping me
Wendelius 10-Jul-15 11:17am    
I'm not sure if I understand you correctly but in the first article, take the example code but don't loop through all columns, just the first one.

Have a look at the updated answer.

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