关于DataGrid的表头问题。定高分相谢!急! http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=0BF1E82C-A30A-4B5B-85B2-8DB339A41D37 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在执行了DataBind()后加入如下代码:Table t = (Table)DataGrid1.Controls[0];DataGridItem row = new DataGridItem(0, -1, ListItemType.Header);t.Rows.AddAt(0, row);TableCell cell1 = new TableCell();cell1.ColumnSpan = 2;cell1.Text = "大类1";row.Cells.Add(cell1);TableCell cell2 = new TableCell();cell2.Text = "大类2";row.Cells.Add(cell2); <!--多维表头,使用Repeater控件,存成aspx文件--><%@ Page Language="C#" %><html> <head> <script language="C#" runat="server"> void Page_Load(Object Sender, EventArgs e) { if (!IsPostBack) { ArrayList values = new ArrayList(); values.Add(new PositionData("Microsoft", "1","400")); values.Add(new PositionData("Intel", "2","200")); values.Add(new PositionData("Dell", "3","50")); values.Add(new PositionData("Sun", "4","-150")); Repeater1.DataSource = values; Repeater1.DataBind(); } } public class PositionData { private string name; private string ticker; private string money; public PositionData(string name, string ticker, string money) { this.name = name; this.ticker = ticker; this.money = money; } public string Name { get { return name; } } public string Ticker { get { return ticker; } } public string Money { get { return money; } } } </script> </head> <body> <h3>Repeater Example</h3> <form runat=server> <b>Repeater1:</b> <p> <asp:Repeater id=Repeater1 runat="server"> <HeaderTemplate> <table border=1> <tr> <td rowspan=2><b>Company</b></td> <td colspan=2><b>财务排名</b></td> </tr> <tr> <td><b>名次</b></td> <td><b>盈利</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Money") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <p> </form> </body> </html> 1、DataGrid显示双层表头假设你的DataGrid有三列,现在想将前两列作为"大类1",第三列作为"大类2",现在,你可以在ItemDataBound事件中加入下面的代码:if (e.Item.ItemType == ListItemType.Header){ e.Item.Cells[0].ColumnSpan = 2; e.Item.Cells[0].Text = "大类1</td><td>大类2</td></tr><tr><td>" + e.Item.Cells[0].Text;}用这个方法可以为任意添加新行。 要注意e.Item.Cells[1].Visible=false; asp.net批量添加IP地址 C#.NET群发邮件问题 插入数据中的汉字变成乱码 在GridView中怎么将一列数据传到另一个也面 GridView标题处理 不知道如何隐藏treeview的子节点,我的treeview是静态的 DataList中绑定的数据如何显示列表的样式? asp.net网站占用服务器内存大的问题 如何在html代码中调用服务器端的后台代码,以便动态的生成相应的html显示? 请问哪里有Microsoft Application Center Test的相关资料?多谢 大讨论Asp.net 在线观注 为什么我的TIMER控件不起作用
Table t = (Table)DataGrid1.Controls[0];
DataGridItem row = new DataGridItem(0, -1, ListItemType.Header);
t.Rows.AddAt(0, row);
TableCell cell1 = new TableCell();
cell1.ColumnSpan = 2;
cell1.Text = "大类1";
row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Text = "大类2";
row.Cells.Add(cell2);
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e) {
if (!IsPostBack) {
ArrayList values = new ArrayList();
values.Add(new PositionData("Microsoft", "1","400"));
values.Add(new PositionData("Intel", "2","200"));
values.Add(new PositionData("Dell", "3","50"));
values.Add(new PositionData("Sun", "4","-150"));
Repeater1.DataSource = values;
Repeater1.DataBind();
}
}
public class PositionData {
private string name;
private string ticker;
private string money;
public PositionData(string name, string ticker, string money) {
this.name = name;
this.ticker = ticker;
this.money = money;
}
public string Name {
get {
return name;
}
} public string Ticker {
get {
return ticker;
}
} public string Money {
get {
return money;
}
}
}
</script>
</head>
<body>
<h3>Repeater Example</h3>
<form runat=server>
<b>Repeater1:</b>
<p>
<asp:Repeater id=Repeater1 runat="server">
<HeaderTemplate>
<table border=1>
<tr>
<td rowspan=2><b>Company</b></td>
<td colspan=2><b>财务排名</b></td>
</tr>
<tr>
<td><b>名次</b></td>
<td><b>盈利</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "Money") %> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<p>
</form>
</body>
</html>
假设你的DataGrid有三列,现在想将前两列作为"大类1",第三列作为"大类2",现在,你可以在ItemDataBound事件中加入下面的代码:
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.Cells[0].ColumnSpan = 2;
e.Item.Cells[0].Text = "大类1</td><td>大类2</td></tr><tr><td>" + e.Item.Cells[0].Text;
}
用这个方法可以为任意添加新行。
e.Item.Cells[1].Visible=false;