DataGrid在ItemDataBound之前我們可以通過DataGrid的ItemCreated事件對表頭進行修改。以達到顯示多層信息的效果
效果圖請查看附件;表頭總共有三行上面兩行是新添加進去的
例如:
private void dglist_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
try
{
ListItemType lit = e.Item.ItemType;
if (ListItemType.Header == lit)
{
e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
}
}
catch (Exception ex)
{
this.ExecJavaScript("alert('" + ex.Message + "');");
}
}
{
try
{
ListItemType lit = e.Item.ItemType;
if (ListItemType.Header == lit)
{
e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
}
}
catch (Exception ex)
{
this.ExecJavaScript("alert('" + ex.Message + "');");
}
}
private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
{
try
{
TableCell cell;
for (int i = 0; i < 9; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "3");
cell.RenderControl(writer);
}
writer.Write("<td colspan=\"5\" align=\"center\">申請人信息</td>");
writer.Write("<td colspan=\"4\" align=\"center\">使用人信息</td>");
cell = (TableCell)ctl.Controls[18];
cell.Attributes.Add("rowspan", "3");
cell.RenderControl(writer);
writer.Write("<td colspan=\"6\" align=\"center\">經辦人信息</td>");
writer.Write("</tr>\n");
this.dglist.HeaderStyle.AddAttributesToRender(writer);
writer.RenderBeginTag("tr");
for (int i = 9; i < 14; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "2");
cell.RenderControl(writer);
}
writer.Write("<td colspan=\"2\" align=\"center\">集團員工</td>");
writer.Write("<td colspan=\"2\" align=\"center\">非集團員工</td>");
for (int i = 19; i < 25; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "2");
cell.RenderControl(writer);
}
writer.Write("</tr>\n");
this.dglist.HeaderStyle.AddAttributesToRender(writer);
writer.RenderBeginTag("tr");
for (int i = 14; i < 18; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.RenderControl(writer);
}
writer.Write("</tr>\n");
}
catch
{
}
}
{
try
{
TableCell cell;
for (int i = 0; i < 9; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "3");
cell.RenderControl(writer);
}
writer.Write("<td colspan=\"5\" align=\"center\">申請人信息</td>");
writer.Write("<td colspan=\"4\" align=\"center\">使用人信息</td>");
cell = (TableCell)ctl.Controls[18];
cell.Attributes.Add("rowspan", "3");
cell.RenderControl(writer);
writer.Write("<td colspan=\"6\" align=\"center\">經辦人信息</td>");
writer.Write("</tr>\n");
this.dglist.HeaderStyle.AddAttributesToRender(writer);
writer.RenderBeginTag("tr");
for (int i = 9; i < 14; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "2");
cell.RenderControl(writer);
}
writer.Write("<td colspan=\"2\" align=\"center\">集團員工</td>");
writer.Write("<td colspan=\"2\" align=\"center\">非集團員工</td>");
for (int i = 19; i < 25; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.Attributes.Add("rowspan", "2");
cell.RenderControl(writer);
}
writer.Write("</tr>\n");
this.dglist.HeaderStyle.AddAttributesToRender(writer);
writer.RenderBeginTag("tr");
for (int i = 14; i < 18; i++)
{
cell = (TableCell)ctl.Controls[i];
cell.RenderControl(writer);
}
writer.Write("</tr>\n");
}
catch
{
}
}