下面是一个示例图:
既有两个表,其中一个是大类表,还有一个是小类表。比如在使用ms sql server的Northwind数据库时,在product产品表中,每一个产品都是属于一个类别,这些类别在category表中定义。那么我们可以按照category表中类别的顺序,在DATAGRID中显示每个类别有哪些产品。
在这个DATAGRID中,蓝色的行标题是一个分类标题,分类标题以下的就是该分类下的所有代管人员了。
首先:建立一个--DataGrid--
<asp:DataGrid id="dg_CusInfo" runat="server" CssClass="GbText" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" AutoGenerateColumns="False" Width="100%"
DataKeyField="EmpTypeID">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="EmpTypeName" HeaderText="代管人员"></asp:BoundColumn>
<asp:BoundColumn DataField="EmpTypeKinds" HeaderText="类 型">
<HeaderStyle HorizontalAlign="Center" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="CreateDate" HeaderText="创建时间" DataFormatString="{0:yyyy-MM-dd}">
<HeaderStyle HorizontalAlign="Center" Width="12%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center" Width="5%"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton runat="server" Text="进入" CommandName="" CausesValidation="false" CssClass="buttoncss"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
然后信息绑定 到DataGrid中

dg_CusInfo信息绑定#region dg_CusInfo信息绑定
public void CusInfoBind()

{
string ID=Session["userid"].ToString();
string type=Session["type"].ToString();
string strSQL = "select EmpTypeName,EmpTypeID,EmpTypeKinds,CreateDate,NameChina from Cus_EmpTypeInfo,CustomerInfo where CusID=CustomerID order by Cus_EmpTypeInfo.CusID";
DbTools db = new DbTools(); //建立数据库连接对象;
DataSet ds = new DataSet();
ds = db.DataSetResult(strSQL,par);
string curCat;//指示当前记录中产品所属的类别
string prevCat = null;//指示上一条记录中产品所属的类别
int i = 0;

//遍历结果集,找出要插入分类标题的行
while ( i<= ds.Tables[0].Rows.Count-1 )

{
curCat = ds.Tables[0].Rows[i]["NameChina"].ToString();
if ( curCat != prevCat)

{//如果发现前后两记录的所属类别不一样
prevCat = curCat;
DataRow shRow = ds.Tables[0].NewRow();
shRow["EmpTypeName"] = ds.Tables[0].Rows[i]["NameChina"].ToString();//修改行的标题为分类标题名
shRow["EmpTypeKinds"] = "SubHead";//设置一个临时的标记,为判断合并作准备
shRow["CreateDate"] = "1900-1-1";
shRow["EmpTypeID"] = 1;
ds.Tables[0].Rows.InsertAt(shRow,i);//插入新的分类标题行
i += 1;
}
i += 1;
}
this.dg_CusInfo.DataSource = ds;
this.dg_CusInfo.DataBind();
db.Close();
}
#endregion
shRow("UnitPrice") = "SubHead",是为了在item_bound事件中判定是否要合并整个行!
其item_bound事件中

dg_CusInfo相关事件#region dg_CusInfo相关事件
private void dg_CusInfo_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{
if ( e.Item.Cells[1].Text.Equals("SubHead"))

{ //设置列宽
e.Item.Cells[0].ColumnSpan = 4;
//合拼为一个新的分类标题行,移除其中的单元格
e.Item.Cells.RemoveAt(3);
e.Item.Cells.RemoveAt(2);
e.Item.Cells.RemoveAt(1);
e.Item.Cells[0].Attributes.Add("align","left");
e.Item.Cells[0].Font.Bold = true;
e.Item.BackColor = Color.FromArgb(204,204,255);
}
}
}
#endregion