[转]对DataTable进行求和、平均等数学运算
转自:浪剑
private void dgMain_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemtype=e.Item.ItemType;
if(itemtype==ListItemType.Footer)
{
DataTable tbTotal=(DataTable)(dgMain.DataSource);
//e.Item.Cells[0].ColumnSpan=2;
e.Item.Cells[1].Visible=false;
e.Item.Cells[0].Text="<B>合 计</B>";
if(tbTotal.Rows.Count>0)
{
e.Item.Cells[2].Text=tbTotal.Compute("sum([留言总数])","1>0").ToString();
e.Item.Cells[3].Text=tbTotal.Compute("sum([公开留言])","1>0").ToString();
e.Item.Cells[4].Text=tbTotal.Compute("sum([个别留言])","1>0").ToString();
e.Item.Cells[6].Text=tbTotal.Compute("sum([家长留言])","1>0").ToString();
}
else
{
e.Item.Cells[2].Text="0";
e.Item.Cells[3].Text="0";
e.Item.Cells[4].Text="0";
e.Item.Cells[6].Text="0";
}
e.Item.Cells[5].Text="";
}
if(itemtype==ListItemType.Item || itemtype==ListItemType.AlternatingItem)
{
e.Item.Cells[1].Text = WebComponents.s2pDataTranslate.GetSchoolTypeNameState(e.Item.Cells[7].Text.ToString()) + "-" + e.Item.Cells[1].Text;
}
}
{
ListItemType itemtype=e.Item.ItemType;
if(itemtype==ListItemType.Footer)
{
DataTable tbTotal=(DataTable)(dgMain.DataSource);
//e.Item.Cells[0].ColumnSpan=2;
e.Item.Cells[1].Visible=false;
e.Item.Cells[0].Text="<B>合 计</B>";
if(tbTotal.Rows.Count>0)
{
e.Item.Cells[2].Text=tbTotal.Compute("sum([留言总数])","1>0").ToString();
e.Item.Cells[3].Text=tbTotal.Compute("sum([公开留言])","1>0").ToString();
e.Item.Cells[4].Text=tbTotal.Compute("sum([个别留言])","1>0").ToString();
e.Item.Cells[6].Text=tbTotal.Compute("sum([家长留言])","1>0").ToString();
}
else
{
e.Item.Cells[2].Text="0";
e.Item.Cells[3].Text="0";
e.Item.Cells[4].Text="0";
e.Item.Cells[6].Text="0";
}
e.Item.Cells[5].Text="";
}
if(itemtype==ListItemType.Item || itemtype==ListItemType.AlternatingItem)
{
e.Item.Cells[1].Text = WebComponents.s2pDataTranslate.GetSchoolTypeNameState(e.Item.Cells[7].Text.ToString()) + "-" + e.Item.Cells[1].Text;
}
}
<ASP:DATAGRID ID="dgMain" RUNAT="server" AUTOGENERATECOLUMNS="False" CSSCLASS="FixTable" WIDTH="545"
SHOWFOOTER="True">
SHOWFOOTER="True">