读书人

求个gridview查询显示结果后某一列或

发布时间: 2013-03-06 16:20:31 作者: rapoo

求个gridview查询显示结果后,某一列或多列分别求和,结果显示在label上
网上搜了半天,gridview的求和代码貌似都是清清月儿那个版本,可是在我这里貌似用不了,会提示“输入字符串的格式不正确”。

前台:


<asp:GridView ID="GridView1" runat="server"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="日期">
<ItemTemplate>
<asp:Label ID="lb_Date" runat="server" Text='<%# Bind("ExpDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="名称">
<ItemTemplate>
<asp:Label ID="lb_Name" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:Label ID="lb_result" runat="server" Text='<%# Bind("ExpResult") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>


<asp:TemplateField HeaderText="收入">
<ItemTemplate>
<asp:Label ID="lb_income" runat="server" Text='<%# Bind("ExpIncome") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>



后台查询显示:


string sConnectionString = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
using (SqlConnection conn1 = new SqlConnection(sConnectionString))
{
conn1.Open();
SqlDataAdapter dap = new SqlDataAdapter("SELECT * FROM [AppExp]", conn1);

DataSet ds1 = new DataSet();
try


{
dap.Fill(ds1);
}
catch
{
}

GridView1.DataSource = ds1;
GridView1.DataBind();



求和显示到label的代码:


private Decimal summoney = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
summoney += Convert.ToDecimal(e.Row.Cells[3].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
lb_summoney.Text = summoney.ToString();

}
}


报错的就是上面这行:summoney += Convert.ToDecimal(e.Row.Cells[4].Text);

我想问下,这个3是不是gridview显示从做往右从0开始数第3列?

我上面那个确实是第3列啊?

请教,谢谢。
[解决办法]
用JQuery操作吧。组织好dom元素的class / id

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var varObj = $(".td1");
var num = 0;
varObj.each(function () {
num += parseInt($(this).text());
});

$(".total").text(num);


});
</script>

<style type="text/css">
.total{ font-size:14px; font-weight:bold; color:#f00;}
</style>

</head>
<body>
第一列总和:<span class="total"></span>
<hr />

<table>
<tr>
<td class="td1_total"></td>
<td class="td2_total"></td>
</tr>
<tr>
<td class="td1">21</td>
<td class="td2">36</td>
</tr>
<tr>
<td class="td1">23</td>
<td class="td2">57</td>
</tr>
<tr>
<td class="td1">47</td>
<td class="td2">23</td>
</tr>
</table>

</body>
</html>


[解决办法]
引用:
引用:用js算和还有有可行性的。比如数量一列加上.amount 样式来找到对象。


如果实在后台的话,可以根据取出的datatbale的compute来算和。(百度查datatable compute)


缺点是只能把当前页的数据之和都算出来, 而无法做到根据选取列来算和

我这个还含有分页的,要求是查询出来……


可以根据取出的datatbale的compute来算和。(百度查datatable compute)
[解决办法]
为什么是在RowDataBound中呢?
这种方式很慢,影响速度。不如在DataBound中用SQL统计呢。

读书人网 >asp.net

热点推荐