实现效果:
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 |
代码:
using
System;
using
System.Data;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Collections.Generic;
![](/Images/OutliningIndicators/None.gif)
public
partial
class
_Default : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack) bindPersons();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//绑定GridView
protected void bindPersons()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.GridView1.DataSource = getSource();
this.GridView1.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
//创建数据源
protected DataTable getSource()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**-----------------------------------------
* Id FisrtName LastName Depart
* -----------------------------------------
* 1 fname1 lname1 Sales
* 1 fname2 lname2 IT
* -----------------------------------------
* */
DataTable persons = new DataTable();
persons.Columns.Add("Id", typeof(System.Int32));
persons.Columns.Add("FirstName", typeof(System.String));
persons.Columns.Add("LastName", typeof(System.String));
persons.Columns.Add("Depart", typeof(System.String));
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 10; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataRow row = persons.NewRow();
row["Id"] = i+1;
row["FirstName"] = "fname" + (i + 1);
row["LastName"] = "lname" + (i + 1);
if (i < 5)
row["Depart"] = "Sales";
else
row["Depart"] = "IT";
persons.Rows.Add(row);
}
return persons;
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//合并header的firstName和lastName列
if (e.Row.RowType == DataControlRowType.Header)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//删除lastName列
e.Row.Cells.RemoveAt(2);
//合并列
e.Row.Cells[1].Attributes["colspan"] = "2";
//设置列标签
e.Row.Cells[1].Text = "<font color=\"red\">Full Name</font>";
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void GridView1_PreRender(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//合并datarow
Dictionary<string,List<GridViewRow>> rowList = new Dictionary<string,List<GridViewRow>>();
foreach (GridViewRow row in GridView1.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string departName = row.Cells[3].Text;
if (!rowList.ContainsKey(departName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rowList[departName] = new List<GridViewRow>();
}
rowList[departName].Add(row);
}
![](/Images/OutliningIndicators/InBlock.gif)
foreach (KeyValuePair<string, List<GridViewRow>> departList in rowList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 0;
foreach (GridViewRow row in departList.Value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (i == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.Cells[3].Attributes["rowspan"] = departList.Value.Count.ToString();
row.Cells[3].Attributes["valign"] = "center";
i++;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
row.Cells.RemoveAt(3);
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
下载:
文件
转载于:https://www.cnblogs.com/oiea/archive/2008/01/28/1055842.html