日期:2014-05-18  浏览次数:20479 次

GridView合并单元格(多列)
不知道合并gridview单元格有没有好的方法,是多列的。
比如我有姓名、照片、性别、出生年月等多列信息,其中这四列都是要合并单元格的。现在的问题就是姓名不太可能重复,但是照片会出现这样一个问题,因为可以不上传照片,所以如果相邻的两条信息都没有上传照片的话,照片这一列就不好合并了。性别的问题更突出,比如相邻的两条信息都是男或者都是女,合并好像也比较复杂。请问有没有好一点的办法?
或者说使用telerik 的radgrid可以实现这个功能吗?

HTML code

原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A    P    M    XXXX    O1      O2      O3
A    P    M    XXXX    O4      O5      O6
A    P    M    XXXX    O7      O8      O9
B    P2   F    YYYY    10      11      12
B    P2   F    YYYY    13      14      15
B    P2   F    YYYY    16      17      18
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
                       O1      O2      O3
A    P    M    XXXX    O4      O5      O6
                       O7      O8      O9
                       10      11      12
B    P2   F    YYYY    13      14      15
                       16      17      18






------解决方案--------------------
C# code

/// <summary>
        /// 合并行(合并的列要用Label控件)
        /// </summary>
        /// <param name="gvw">需要合并的GridView</param>
        /// <param name="sCol">要合并的列(从0开始)</param>
        /// <param name="controlName">控件名称</param>
        public static void MergeRows(GridView gvw, int col, string controlName)
        {
            for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow row = gvw.Rows[rowIndex];

                GridViewRow previousRow = gvw.Rows[rowIndex + 1];

                Label row_lbl = row.Cells[col].FindControl(controlName) as Label;
                Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label;

                if (row_lbl != null && previousRow_lbl != null)
                {
                    if (row_lbl.Text == previousRow_lbl.Text)
                    {
                        row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1;

                        previousRow.Cells[col].Visible = false;
                    }
                }
            }
        }

------解决方案--------------------
这样的页面最好用table来做,然后ajax去后台取数据这样的灵活性大一点。
------解决方案--------------------
一般都用Table 好操作
------解决方案--------------------
C# code

    protected void Page_Load(object sender, EventArgs e)
    {
//...
        GridView1.DataSource = dt;
        GridView1.DataBind();
        GroupName(0);
        GroupName(1);
        GroupName(3);
        GroupSex();
    }
    public void GroupName(int col)
    {
        TableCell oldName = GridView1.Rows[0].Cells[col];
        for (int i = 1; i < GridView1.Rows.Count; i++)
        {
            TableCell Name = GridView1.Rows[i].Cells[col];
            if (oldName.Text == Name.Text)
            {
                Name.Visible = false;
                if (oldName.RowSpan == 0)
                {
                    oldName.RowSpan = 1;
                }
                oldName.RowSpan++;
                oldName.VerticalAlign = VerticalAlign.Middle;
            }
            else
            {
                oldName = Name;
            }
        }
    }
    public void GroupSex()
    {
        TableCell oldName = GridView1.Rows[0].Cells[0];
        TableCell oldSex = GridView1.Rows[0].Cells[2];
        for (int i = 1; i < GridView1.Rows.Count; i++)
        {
            TableCell Name = GridView1.Rows[i].Cells[0];
            TableCell Sex = GridView1.Rows[i].Cells[2];
            if (oldName.Text == Name.Text && oldSex.Text == Sex.Text)
            {
                Sex.Visible = false;
                if (oldSex.RowSpan == 0)
                {