Asp.net GridView页脚不显示数据
我正在使用ASP.NET(VB.NET)和SQL-Server-2012 我使用GridView在名为Asp.net GridView页脚不显示数据,asp.net,vb.net,gridview,sql-server-2012,Asp.net,Vb.net,Gridview,Sql Server 2012,我正在使用ASP.NET(VB.NET)和SQL-Server-2012 我使用GridView在名为project\u items的表中显示数据 我添加了一个页脚模板,以便在一列中显示所有记录的总数 这就是我所做的: <asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Names="Tahoma" ForeColor="#333333" GridLines
project\u items
的表中显示数据
我添加了一个页脚模板,以便在一列中显示所有记录的总数
这就是我所做的:
<asp:GridView ID="grdItems" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Names="Tahoma" ForeColor="#333333" GridLines="None" ShowFooter="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="item_name" HeaderText="Item" SortExpression="item_name" />
<asp:BoundField DataField="item_cost" HeaderText="Cost (inc. VAT)" SortExpression="item_cost" />
<asp:BoundField DataField="item_quantity" HeaderText="Quantity" SortExpression="item_quantity" />
<asp:TemplateField HeaderText="Sub-Total (inc. VAT)">
<ItemTemplate>
<asp:Label ID="TextBox3" runat="server"
Text='<%# Convert.ToInt32(Eval("item_quantity")) * Convert.ToDouble(Eval("item_cost"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalPrice" runat="server" />
</FooterTemplate>
<FooterTemplate>
<asp:Label ID="lblPrice" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT
items.item_name, items.item_cost, project_items.item_quantity
FROM items
INNER JOIN project_items ON items.item_id = project_items.item_id
WHERE project_items.project_id = @parameter">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="ProjectID" />
</SelectParameters>
</asp:SqlDataSource>
问题是页脚没有显示总金额(或任何值)
如何修复此问题?您的行数据绑定不太正确 RowDataBound将为每一行触发。因此,当您检查一个数据行时,一开始的总数是0。每次通话都会重新设置。因此,当您最终到达页脚时,行总数仍然为0 相反,请执行以下操作: 在页面加载之外声明一个私有小数:
Private myTotal As Decimal = 0
然后在数据绑定中,将总数相加并存储在total
然后在页脚中使用它。差不多
Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
lblTotalPrice.Text = myTotal.ToString()
End If
End Sub
更新
在gridview中没有lblPrice
。因此,如果在myTotal+=价格上设置一个断点,则表示将0添加到0
查看上面的代码,了解获取价格的不同方法。我按照您所说的那样尝试了,但页脚仍然是空的。我错过了什么?(有问题的更新代码)嗨,很抱歉打扰你。同样,页脚不显示。我的ASP代码有问题吗?正在显示页脚;你可以从你的形象中看到这一点。错的是你试图把一个不存在的数字加起来。gridview中没有
lblPrice
,因此它会将0添加到myTotal
尝试在代码中添加一些断点,这样您就可以确切地看到调用了什么以及设置了什么total。另请参见我答案中的更新代码,它向您展示了如何从您的dataitem
中获取item\u成本。我添加了一个lblPrice,但仍然没有显示任何内容(请参阅更新的代码)
Protected Sub grdItems_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
myTotal += (CDec(rowView("item_cost")) * CDec(rowView("item_quantity")))
End If
If e.Row.RowType = DataControlRowType.Footer Then
Dim lblTotalPrice As Label = DirectCast(e.Row.FindControl("lblTotalPrice"), Label)
lblTotalPrice.Text = myTotal.ToString()
End If
End Sub