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页面中设置<FooterTemplate>来实现的话,就会发现所得到的效果是在每行数据中都会多出一个空白列,如图:
先前走了一些弯路,把这个问题想得很复杂了,之后看了MSDN的里面的相关例子,才发现原来2003Datagrid里面要实现这个功能比在gridview要难得多,英文的MSDN比中文的MSDN好使多了,建议大家多用MSDN en版了:)
我把参考的MSDN的文件地址贴出来,供大家参考:
http://msdn.microsoft.com/library/en-us/ ... dViewEx09.asp?frame=true
顺便把其vb的代码,c#的代码也贴过来,方便以后参照使用
vb.net
Dim
priceTotal
As
Decimal
=
0
Dim
quantityTotal
As
Integer
=
0

Sub detailsGridView_RowDataBound()
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#
"
%>

<
script runat
=
"
server
"
>

void
CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)

...
{
if(e.Row.RowType == DataControlRowType.DataRow)

...{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}

</
script
>

<
html
>
<
body
>
<
form runat
=
"
server
"
>
<
h3
>
GridView RowDataBound Example
</
h3
>

<
asp:gridview id
=
"
CustomersGridView
"
datasourceid
=
"
CustomersSqlDataSource
"
autogeneratecolumns
=
"
true
"
allowpaging
=
"
true
"
onrowdatabound
=
"
CustomersGridView_RowDataBound
"
runat
=
"
server
"
>
</
asp:gridview
>
<!--
This example uses Microsoft SQL Server and connects
-->
<!--
to the Northwind sample database. Use an ASP.NET
-->
<!--
expression to retrieve the connection
string
value
-->
<!--
from the Web.config file.
-->
<
asp:sqldatasource id
=
"
CustomersSqlDataSource
"
selectcommand
=
"
Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]
"
connectionstring
=
"
<%$ ConnectionStrings:NorthWindConnectionString%>
"
runat
=
"
server
"
>
</
asp:sqldatasource
>
</
form
>
</
body
>
</
html
>

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

大家看在最下面的一行,只出现了一个数值,其它列都不存在数值,而这个数的功能主要是对上面这行"持仓股票市值进行一个总的统计",这是如何实现的呢?
首先,我们要把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页面中设置<FooterTemplate>来实现的话,就会发现所得到的效果是在每行数据中都会多出一个空白列,如图:

先前走了一些弯路,把这个问题想得很复杂了,之后看了MSDN的里面的相关例子,才发现原来2003Datagrid里面要实现这个功能比在gridview要难得多,英文的MSDN比中文的MSDN好使多了,建议大家多用MSDN en版了:)
我把参考的MSDN的文件地址贴出来,供大家参考:
http://msdn.microsoft.com/library/en-us/ ... dViewEx09.asp?frame=true
顺便把其vb的代码,c#的代码也贴过来,方便以后参照使用
vb.net
程序代码: |
























c#.net
程序代码: |




























在vs2005中提供的MSDN对 GridView.RowDataBound 事件 的描述是这样的:
呈现 GridView 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 GridViewRow 对象表示)绑定到 GridView 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如修改绑定到该行的数据的值)。
它也提供了一个example出来
程序代码: | [ 复制代码到剪贴板 ] |


















































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