给Gridview添加了固定表头,但是错位了。。。 本帖最后由 linjiangxian11 于 2012-10-24 19:52:23 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 根据需要修改样式也可通过 protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { TableCellCollection tcHeader = e.Row.Cells; tcHeader.Clear(); tcHeader.Add(new TableHeaderCell()); tcHeader[0].Attributes.Add("rowspan", "2"); tcHeader[0].Text = "企业名称"; tcHeader.Add(new TableHeaderCell()); ...实现 }} 推荐你用 templateHeader 也就是模板列做, asp.net中 GridView 的模板列 功能可以兼容 HTML。 会用 GridView 的模板列吗 这个不会错位,而且可以各种 美化简单的 CSS 就可以解决 漂亮的 头 能否具体举例说明啊,templateHeader 这个我还真没用过 你的意思是不是把Gridview的各个列转换成模板,再在HeaderTemplate中设计(假如我在HeaderTemplate中加入了一个Label)这个Label的尺寸呢? 设置每列的宽度,以及gridview总的宽度等等 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title></head><body> <form id="form1" runat="server"> <table> <tr> <td> <asp:GridView ID="GridView2" runat="server" Font-Size="12px" GridLines="Both" CellPadding="4" Width="560px" ForeColor="#333333" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource2"> <Columns> <asp:BoundField DataField="ContractNumberID" HeaderText="合同ID"" ReadOnly="True" SortExpression="ContractNumberID" /> <asp:BoundField DataField="ContractNumber" HeaderText="合同编号" SortExpression="ContractNumber" /> <asp:BoundField DataField="LeaseDateFrom" HeaderText="起始日" SortExpression="LeaseDateFrom" /> <asp:BoundField DataField="Month" HeaderText="Month" SortExpression="Month" /> <asp:BoundField DataField="MonthRent" HeaderText="MonthRent" SortExpression="MonthRent" /> <asp:BoundField DataField="MonthRentPaid" HeaderText="MonthRentPaid" SortExpression="MonthRentPaid" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#E3EAEB" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#EDEDED" Height="26px" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" SelectCommand="SELECT [ContractNumberID], [ContractNumber], [LeaseDateFrom], [Month], [MonthRent], [MonthRentPaid] FROM [RentTableA]"></asp:SqlDataSource> </td> </tr> </table> </form></body></html> 我仍使用顶楼的Script脚本,并给各个模板又添加了HeaderTemplate,并设置了长度!本机测试成功了!Gridview虽然加入了大量的TextBox等控件,但能完美的显示!然而当我用其他浏览器或其他机器打开来看时!表头又变成长短不一,我彻底纠结了!请问,有没有完整正确的示例啊!网上用CSS的例子没一个试成功的。 ASP.NET 提示找不到控件,右键-转到定义,提示“对象定义被隐藏”,该解决? if-else逻辑处理问题 看了一篇文章关于Server.Transfer传值。 有问题请教 asp.net页面统一继承与namespace更名的问题!! 一个nhibenate的hql问题! 关于事件丢失的问题 有人用Web Matri開發Asp.net嗎? 800元请高手帮忙,信息真实可靠请版主勿删,急急急! 一个windows应用程序怎样用于web?添加到页面中去 关于asp.net 2.0 部署问题! cache全局共享, 多帐套如何处理? 如何禁止用户在网站里下载文件
也可通过
protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[0].Text = "企业名称";
tcHeader.Add(new TableHeaderCell());
...实现
}}
能否具体举例说明啊,templateHeader 这个我还真没用过
你的意思是不是把Gridview的各个列转换成模板,再在HeaderTemplate中设计(假如我在HeaderTemplate中加入了一个Label)这个Label的尺寸呢?
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title></head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:GridView ID="GridView2" runat="server" Font-Size="12px"
GridLines="Both" CellPadding="4" Width="560px" ForeColor="#333333"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource2">
<Columns> <asp:BoundField DataField="ContractNumberID" HeaderText="合同ID"" ReadOnly="True" SortExpression="ContractNumberID" />
<asp:BoundField DataField="ContractNumber" HeaderText="合同编号" SortExpression="ContractNumber" />
<asp:BoundField DataField="LeaseDateFrom" HeaderText="起始日" SortExpression="LeaseDateFrom" />
<asp:BoundField DataField="Month" HeaderText="Month" SortExpression="Month" />
<asp:BoundField DataField="MonthRent" HeaderText="MonthRent" SortExpression="MonthRent" />
<asp:BoundField DataField="MonthRentPaid" HeaderText="MonthRentPaid" SortExpression="MonthRentPaid" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#EDEDED" Height="26px" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>"
SelectCommand="SELECT [ContractNumberID], [ContractNumber], [LeaseDateFrom], [Month], [MonthRent], [MonthRentPaid] FROM [RentTableA]"></asp:SqlDataSource>
</td>
</tr>
</table> </form>
</body>
</html>