Rafael Oropeza
Rafael Oropeza

Reputation: 11

how to merge columns with same value in every row gridview asp.net

I have this gridview:

enter image description here

But need this:

enter image description here

I need to merge the columns for every road when they have the same value!!! I'm trying to make an Schedule with the gridview.

Thanks a lot for your help...

Upvotes: 0

Views: 1154

Answers (1)

Rafael Oropeza
Rafael Oropeza

Reputation: 11

Here is the solution I made myself...

If any one else needs it...

for (int x = 0; x < e.Row.Cells.Count; x++)
    {
        // work out how wide the cell span should be for this cell
        int span = 1;
        while (

            (x + span < e.Row.Cells.Count) && 
            (e.Row.Cells[x + span].Text == e.Row.Cells[x].Text) &&
            (e.Row.Cells[x].Text != "&nbsp;")
            )
        {
            span++;
        }

        // if we need to span this cell
        if (span > 1)
        {
            // apply the span value to the first cell in the group
            e.Row.Cells[x].ColumnSpan = span;

            for (int n = 1; n < span; n++)
            {
                while (n < span)
                {
                    e.Row.Cells[x + span - n].Visible = false;
                    n++;
                }
            }
            // skip to the next cell past the end of the span
            x += span - 1;
        }
    }

Upvotes: 1

Related Questions