close
實現效果:
IdFull NameDepart
1fname1lname1Sales
2fname2lname2
3fname3lname3
4fname4lname4
5fname5lname5
6fname6lname6IT
7fname7lname7
8fname8lname8
9fname9lname9
10fname10lname10


protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {//合併header的firstName和lastName列
       
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //刪除lastName列
            e.Row.Cells.RemoveAt(2);
            //合併列
            e.Row.Cells[1].Attributes["colspan"] = "2";
            //設置列標籤
            e.Row.Cells[1].Text = "<font color=\"red\">Full Name</font>";
        }

    }

 

 

 protected void GridView1_PreRender(object sender, EventArgs e)
    {//合併datarow
       
        Dictionary<string,List<GridViewRow>> rowList = new Dictionary<string,List<GridViewRow>>();
        foreach (GridViewRow row in GridView1.Rows)
        {
            string departName = row.Cells[3].Text;
            if (!rowList.ContainsKey(departName))
            {
                rowList[departName] = new List<GridViewRow>();
            }
            rowList[departName].Add(row);
        }

        foreach (KeyValuePair<string, List<GridViewRow>> departList in rowList)
        {
            int i = 0;
            foreach (GridViewRow row in departList.Value)
            {

                if (i == 0)
                {
                    row.Cells[3].Attributes["rowspan"] = departList.Value.Count.ToString();
                    row.Cells[3].Attributes["valign"] = "center";
                    i++;
                }
                else
                {
                    row.Cells.RemoveAt(3);
                }
            }
        }

    }
}

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 11 的頭像
    11

    冠霖的部落格

    11 發表在 痞客邦 留言(0) 人氣()