GridView 相同列内容合并单元格 (任意列)

 private void SpanGrid()
    {
        int j = GridView1.Rows.Count;//行数
        int u;//自循环所用参数
        for (int  p= 0; p < GridView1.Columns.Count; p++)
        {
            for (int i = 0; i < j; i++)
            {
                int inRowSpan = 1;//跨行数
                string ItmStr = GridView1.Rows[i].Cells[p].Text.ToString();
                for (u = i + 1; u < j; u++)
                {
                    if (ItmStr.Equals(GridView1.Rows[u].Cells[p].Text.ToString()))
                    {
                        inRowSpan += 1;
                        GridView1.Rows[i].Cells[p].RowSpan = inRowSpan;//对最外层所循环行赋要跨行数
                        GridView1.Rows[u].Cells[p].Visible = false;//当前被跨行不可见
                    }
                    else
                    {
                        break;
                    }
                }
                i = u - 1;
            }
        }
    }

你可能感兴趣的:(GridView 相同列内容合并单元格 (任意列))