GridView增加一个统计行的方法

CMB项目中要涉及到stock的统计功能,由于是采用了gridview来实现数据的显示,这里就碰到了一个问题,在需求分析里客户要求对所有的股票进行一个统计,如下图:



大家看在最下面的一行,只出现了一个数值,其它列都不存在数值,而这个数的功能主要是对上面这行"持仓股票市值进行一个总的统计",这是如何实现的呢?

首先,我们要把gridview里面的属性中ShowFooter="True",就是把gridview的页脚打开,这只是第一步。
第二步:在双击属性面板中的事件,让他自动生成一个GridView1_RowDataBound的事件,我们最终就是要在里面写几行简单的代码实现功能了.
第三步:在protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)加入代码了,由于我这里是做需求分析时,只要在页面里显示出效果就可以了,所以我的代码比较简单。但是如果你要加上统计功能的话,你就可以在里面自定义一些相关变量,或调用相关的方法就可以了,我这里只是一个框架了.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        decimal totalstock=0;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           // totalstock += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "stockholdmarketprice"));

            // totalstock += DataBinder.Eval(e.Row.DataItem, "stockholdmarketprice");

           //在这里就可以实现总和的计算了

        }
        else if(e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[3].Text="持仓总市值";
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].Text = "HKD15,000,000";

        }

这里如果不使用这个事件的话,只在设计的aspx页面中设置来实现的话,就会发现所得到的效果是在每行数据中都会多出一个空白列,如图:



先前走了一些弯路,把这个问题想得很复杂了,之后看了MSDN的里面的相关例子,才发现原来2003Datagrid里面要实现这个功能比在gridview要难得多,英文的MSDN比中文的MSDN好使多了,建议大家多用MSDN en版了:)

我把参考的MSDN的文件地址贴出来,供大家参考:
http://msdn.microsoft.com/library/en-us/dnaspp/html/GridViewEx09.asp?frame=true
顺便把其vb的代码,c#的代码也贴过来,方便以后参照使用

vb.net

Dim priceTotal As Decimal = 0
Dim quantityTotal As Integer = 0
Sub detailsGridView_RowDataBound(ByVal sender As Object, _
  ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' add the UnitPrice and QuantityTotal to the running total variables
        priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
          "UnitPrice"))
        quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
          "Quantity"))
    ElseIf e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(0).Text = "Totals:"
        ' for the Footer, display the running totals
        e.Row.Cells(1).Text = priceTotal.ToString("c")
        e.Row.Cells(2).Text = quantityTotal.ToString("d")
        
        e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
        e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
        e.Row.Font.Bold = True
    End If
End Sub



c#.net



decimal priceTotal = 0;
int quantityTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // add the UnitPrice and QuantityTotal to the running total variables
        priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
          "UnitPrice"));
        quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
          "Quantity"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
        // for the Footer, display the running totals
        e.Row.Cells[1].Text = priceTotal.ToString("c");
        e.Row.Cells[2].Text = quantityTotal.ToString("d");
        
        e.Row.Cells[1].HorizontalAlign = _
          e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Font.Bold = true;
    }
}


在vs2005中提供的MSDN对GridView.RowDataBound 事件 的描述是这样的:

呈现 GridView 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 GridViewRow 对象表示)绑定到 GridView 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如修改绑定到该行的数据的值)。

它也提供了一个example出来



<%@ Page language="C#" %>




  
    

        
      

GridView RowDataBound Example



              datasourceid="CustomersSqlDataSource"
        autogeneratecolumns="true"
        allowpaging="true"
        onrowdatabound="CustomersGridView_RowDataBound"
        runat="server">
      

            
      
      
      
      
              selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      

            
            
    

  




工作中的一点小收获,请大家多多指教.


上一篇: ASP.Net学习之XML读写操作(1)
下一篇: BBS签名(2)
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 1 | 引用: 395 | 查看次数: 30813
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭