gridview 通过objectdatasource绑定到datatable,datatable中有一个合计行,如何让该行不参与排序
gridview 通过objectdatasource绑定到datatable,datatable中有一个合计行,如何让该行不参与排序
[解决办法]
页脚数据不要放在DataTable中,应该单独将合计行的数据插入页脚。
你可以自己从GridView继承,并开发成自己的GridView控件。
[解决办法]
<asp:GridView ID="gvProd" runat="server" DataSourceID="sdsProd" AllowPaging="true" PageSize="20"
OnDataBound="CountTotalPrice" AutoGenerateColumns="false" ShowFooter="true">
<HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="true" />
<FooterStyle BackColor="Black" ForeColor="White" Font-Bold="true" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="Price" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" />
</Columns>
</asp:GridView>
protected void CountTotalPrice(object sender, EventArgs e)
{
decimal totalValue = 0;
foreach (GridViewRow row in gvProd.Rows)
{
decimal price = Decimal.Parse(row.Cells[2].Text);
int count = int.Parse(row.Cells[3].Text);
totalValue += price * count;
}
GridViewRow footer = gvProd.FooterRow;
footer.Cells[0].ColumnSpan = 3;
footer.Cells[0].HorizontalAlign = HorizontalAlign.Center;
footer.Cells.RemoveAt(1);
footer.Cells.RemoveAt(2);
footer.Cells[0].Text = "Total value (on this page): " + totalValue.ToString();
}