ASP.Net Telerik RadGrid动态生成表头
Telerik RadControlsForASP.NET.AJAX是一款很强大的第三方控件,RadGrid是里面用的最多的控件。在旧版本中,要想生成多行多列的表头,应该重写GridBenDept_ItemCreated方法,在内部重新绘制表头。具体实现如下。
/// <summary> /// 创建表头 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridBenDept_ItemCreated(object sender, GridItemEventArgs e) { TableHeaderCell tableCell; switch (e.Item.ItemType) { case Telerik.Web.UI.GridItemType.Header: //第一行表头 TableCellCollection tcHeader = e.Item.Cells; tcHeader.Clear(); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("rowspan", "2"); tableCell.Text = string.Empty; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "1"); tableCell.Text = string.Empty; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("rowspan", "2"); tableCell.Text = "部门编号"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("rowspan", "2"); tableCell.Text = "<Center>部门名称</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "2"); tableCell.Text = "<Center>列1</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "2"); tableCell.Text = "<Center>列2</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "2"); tableCell.Text = "<Center>列3<Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "2"); tableCell.Text = "<Center>列4</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("colspan", "2"); tableCell.Text = "<Center>列5</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Attributes.Add("rowspan", "2"); tableCell.Text = "<Center>操作</Center>" + "</th></tr><tr>"; tcHeader.Add(tableCell); //第二行表头 for (int i = 0; i < 5; i++) { tableCell = new TableHeaderCell(); tableCell.Text = "<Center>XX</Center>"; tcHeader.Add(tableCell); tableCell = new TableHeaderCell(); tableCell.Text = "<Center>XX</Center>"; tcHeader.Add(tableCell); } break; } }
生成的表头截图如下: