0

我在网上搜索了各种示例,并尝试了其中的每一个。无论如何,我都会遇到同样的错误。我正在尝试将标题行添加到代码中的 GridView 控件。我尝试在从 gridview prerender 到页面的所有事件的每个可能事件中添加以下代码。同样的交易。总是得到错误: 表格必须包含按页眉、正文和页脚顺序排列的行部分。

我已将页面精简到最基本的部分 - 删除母版页和所有 CSS。

这是aspx和网格视图代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Shipping.Admin.Default" Title="Apps - Shipping" %>
<html>
<head><title></title></head>
<body>
<form runat="server" id="form1">
<br />
<h1>Admin Page</h1>
<br />

            <asp:GridView ID="gvShipments" runat="server" AllowSorting="True" 
                AutoGenerateColumns="False" onsorting="gvShipments_Sorting" Width="100%" 
                AllowPaging="True" onpageindexchanging="gvShipments_PageIndexChanging" 
                PageSize="50">                 
                 <PagerSettings Position="TopAndBottom" />
                <Columns>
                    <asp:BoundField DataField="RequestDate" HeaderText="Request Date" SortExpression="dtRequestDate" />
                    <asp:BoundField DataField="Requestor" HeaderText="Requestor" SortExpression="Requestor" />
                    <asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
                    <asp:BoundField DataField="ShipmentDescription" HeaderText="Description" SortExpression="ShipmentDescription" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                    <asp:BoundField DataField="ShipmentType" HeaderText="Shipment Type" SortExpression="ShipmentType" />
                    <asp:BoundField DataField="ServiceLevel" HeaderText="Service Level" SortExpression="ServiceLevel" />
                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                    <asp:HyperLinkField  DataNavigateUrlFields="ShipmentId" DataNavigateUrlFormatString="Shipment.aspx?CatId=Admin&amp;sID={0}" Text="&nbsp;&nbsp;&nbsp;edit" />
               </Columns>
           </asp:GridView>       
<br />
</form>
</body>
</html>

这是代码隐藏的片段。此方法在 Page_Load 方法中调用:

private void LoadGridView()
        {
            DataSet ds = new DataSet();
            ds = GetDataset();

            DataTable dtRequests = ds.Tables["Admin"];
            DataView dv = new DataView(dtRequests);

            if (ViewState["sortexpression"] != null)
            {
                dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
            }
            else
            {
                dv.Sort = "dtRequestDate DESC";
            }

            gvShipments.DataSource = dv;
            gvShipments.DataBind();

            **if (gvShipments.Rows.Count > 0)
            {
                this.gvShipments.UseAccessibleHeader = true;
                this.gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
            }**    

            ds.Dispose();
        }

我尝试在任何地方添加粗体的 IF 语句中的代码。我难住了。请帮忙!

谢谢

4

1 回答 1

2

试试这个添加 Page_PreRender事件并在其中发布您的代码,然后您的代码如下所示

protected void Page_PreRender(object sender, EventArgs e)
 {
     if (gvShipments.Rows.Count > 0)
     {
         gvShipments.UseAccessibleHeader = true;
         gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
     }
}

页面生命周期 在此处输入图像描述

于 2013-10-02T06:10:21.017 回答