Gridview初始化后TH/TD为空

本文关键字:TD 为空 TH 初始化 Gridview | 更新日期: 2023-09-27 18:08:56

<asp:GridView ID="GridView_Stores" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" AllowSorting="false" HTMLEncoded="false" AutoGenerateColumns="true" DataSourceID="myDataSource" ShowHeaderWhenEmpty="True"/>...

无论源/连接/自动生成与否,body头或项模板的第一行总是包含空白的<tr><th scope="col"><tr><td>;它不会结束,也不会终止。我找不到删除的方法。

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
  {
    string ptn = "$#,###;<span style='color:rgb(255,0,0);'>($#,###)</span>;-";
    if (e.Row.RowType == DataControlRowType.DataRow) 
    {
      SalesTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "sales"));
      RentalTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "rental"));
      PartsTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "parts"));
      ServiceTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "service"));
      OverallTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "total"));
    }
    if (e.Row.RowType == DataControlRowType.Footer) 
    {
      Literal TtalSales = (Literal)e.Row.FindControl("TtalSales");
      TtalSales.Text = SalesTotal.ToString(ptn);
      Literal TtalRental = (Literal)e.Row.FindControl("TtalRental");
      TtalRental.Text = RentalTotal.ToString(ptn);
      Literal TtalParts = (Literal)e.Row.FindControl("TtalParts");
      TtalParts.Text = PartsTotal.ToString(ptn);
      Literal TtalService = (Literal)e.Row.FindControl("TtalService");
      TtalService.Text = ServiceTotal.ToString(ptn);
      Literal TtalTotal = (Literal)e.Row.FindControl("TtalTotal");
      TtalTotal.Text = OverallTotal.ToString(ptn);
    }
  }

Gridview初始化后TH/TD为空

您可以手动定义标题列:

<asp:GridView ID="GridView_Stores" runat="server" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" AllowSorting="false" HTMLEncoded="false" AutoGenerateColumns="false" DataSourceID="myDataSource" ShowHeaderWhenEmpty="true">
    <Columns>
        <asp:BoundField HeaderText="Sales Total" />
        <asp:BoundField HeaderText="Rental Total" />
        <asp:BoundField HeaderText="Parts Total" />
        <asp:BoundField HeaderText="Service Total" />
        <asp:BoundField HeaderText="Overall Total" />
    </Columns>
</asp:GridView>