Id | Full Name | Depart | |
---|---|---|---|
1 | fname1 | lname1 | Sales |
2 | fname2 | lname2 | |
3 | fname3 | lname3 | |
4 | fname4 | lname4 | |
5 | fname5 | lname5 | |
6 | fname6 | lname6 | IT |
7 | fname7 | lname7 | |
8 | fname8 | lname8 | |
9 | fname9 | lname9 | |
10 | fname10 | lname10 |
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);
}
}
}
}
}
留言列表