Opendr's dotNet

技术方便生活

DataGrid 汇总行

定义保存变量保存汇总值 
 private double myTotalMoney;
 private double myTotalNum;


在ItemDataBound中处理事件(由于在Footer中显示,必须设置Footer可见)

  private void grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  
{
   
switch ((int)(e.Item.ItemType))
   
{
    
case (int)ListItemType.Item :
    
case (int)ListItemType.AlternatingItem: 
     
//Calculate total for the field of each row and alternating row.
     myTotalNum += Convert.ToDouble(e.Item.Cells[3].Text);
     myTotalMoney 
+= Convert.ToDouble(e.Item.Cells[4].Text);
     
//Format the data, and then align the text of each cell to the right.
     e.Item.Cells[4].Text = Convert.ToDouble(e.Item.Cells[4].Text).ToString("##,##0.00");  
     e.Item.Cells[
4].Attributes.Add("align""right");
     
break;
    
case (int)ListItemType.Footer:
     
//Use the footer to display the summary row.
     e.Item.Cells[1].Text = "总 计";
     e.Item.Cells[
1].Attributes.Add("align""left");
     e.Item.Cells[
3].Attributes.Add("align""left");
     e.Item.Cells[
4].Attributes.Add("align""right");
     e.Item.Cells[
3].Text = myTotalNum.ToString();
     e.Item.Cells[
4].Text = myTotalMoney.ToString("c");
     
break;
   }


  }

posted on 2006-10-14 09:24    阅读(603)  评论(0)    收藏  举报

导航