see:
Summary Rows in DataGrid Controls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data01102002.asp
Summary Rows in DataGrid Controls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data01102002.asp
还有这里假设你的值为可以转化为整形的。
在ItemDataBound事件里
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( e.Item.ItemIndex >= 0 )
{
for ( int i = 1; i < e.Item.Cells.Count; i++ )
{
if ( e.Item.Cells[i].Text.ToString().Trim() != " " )
{
MyCount[i] += int.Parse( e.Item.Cells[i].Text );
}
}
}
else if (e.Item.ItemType == ListItemType.Footer)
{
e.Item.Cells[0].Text = "合计";
e.Item.Cells[0].Font.Bold = true;
e.Item.Cells[0].HorizontalAlign = HorizontalAlign.Center;
for ( int i = 1; i < e.Item.Cells.Count; i++ )
{
//MyCount[i]现在存的是累加值,你可以求平均数
e.Item.Cells[i].Text = MyCount[i].ToString();
e.Item.Cells[i].Font.Bold = true;
}
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebForm1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" Font-Size="8pt" Font-Names="Verdana" AllowPaging="True" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="单价" HeaderText="单价"></asp:BoundColumn>
<asp:BoundColumn DataField="数量" HeaderText="数量"></asp:BoundColumn>
<asp:BoundColumn DataField="税率" HeaderText="税率"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="合计">
<ItemTemplate>
<%# TotalNum(int.Parse(DataBinder.Eval(Container.DataItem,"id").ToString()))%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>
//.cspublic string TotalNum(int id){
//算出合计结果
}
DataRow dr = DataTable.NewRow();
DataTable.Rows.Add(dr);
DataTable.Rows[RowCount][0] ="合计";
int TotalAmount ;
for(k = 0;k < RowCount - 1; k++)
{
if(DataTable.Rows[k][1].GetType() != System.Type.GetType("System.DBNull"))
{
TotalAmount += Convert.ToInt32(DataTable.Rows[k][1]);
}
}
DataTable.Rows[RowCount][1] = TotalAmount;
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
Dim Cnn as OleDbConnectionPrivate Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
Dim da As New OleDbDataAdapter("select top 5 * from products_2", cnn)
Dim ds As New DataSet
da.Fill(ds, "Products")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End SubFunction GetTotalPrice()
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\web_article\testcodes\14_addingcontrolstodatagridfooteritem\data.mdb"
Dim cnn As New OleDbConnection(connstr)
'Dim TotalPrice as Integer
Dim TotalPrice as double
Dim CmdDataCount as OleDbCommand
Cnn.Open()
CmdDataCount = New OleDbCommand("SELECT SUM(unitprice) FROM products_2",Cnn)
TotalPrice = CmdDataCount.ExecuteScalar()
Cnn.Close
Return (TotalPrice)
End FunctionPrivate Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(2).Text = "Total "
e.Item.Cells(2).HorizontalAlign = HorizontalAlign.Right
Dim oTextbox As New TextBox
oTextbox.Width = New Unit(100, UnitType.Pixel)
'oTextbox.Text = "90.3500dd" 'This can be changed to populate from some datasource.
oTextbox.Text = GetTotalPrice()
e.Item.Cells(3).Controls.Add(oTextbox)
End If
End Sub</script>
<Form runat="server">
<asp:datagrid id="DataGrid1"
runat="server"
width="500px"
AllowPaging="False"
ItemStyle-CssClass="tableItem"
HeaderStyle-CssClass="tableHeader"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
DataKeyField="ProductID"
ShowFooter="True"
BorderWidth="0"
OnItemCreated="DataGrid1_ItemCreated"
>
<Columns>
<asp:TemplateColumn headertext="Product ID">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductID" runat="server"
Text='<%# Container.DataItem("ProductID") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:200px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("ProductName") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Quantity" FooterStyle-HorizontalAlign=Right >
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("QuantityPerUnit") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn headertext="Product Price" ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice" runat="server"
Text='<%# Container.DataItem("UnitPrice") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</Form>