如何在每行gridview asp.net中合并具有相同值的列

时间:2017-02-01 19:55:26

标签: asp.net gridview

我有这个gridview:

enter image description here

但需要这个:

enter image description here

我需要合并每条道路的列,因为它们具有相同的值!我试图用gridview制作一个时间表。

非常感谢你的帮助......

1 个答案:

答案 0 :(得分:1)

以下是我自己制定的解决方案......

如果有其他人需要它......

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