网上搜了半天,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列啊?请教,谢谢。
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列啊?请教,谢谢。
如果实在后台的话,可以根据取出的datatbale的compute来算和。(百度查datatable compute)
缺点是只能把当前页的数据之和都算出来, 而无法做到根据选取列来算和
或者
gridview.findcontrol("这里写你要获取值的那个label的id");
<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>
你第四列是 Decimal类型的字符串吗??
从0开始数的第4列是价格,用的就是Decimal类型的字段,数据库是这样的,但是查询出来显示在gridvirw这个类型不会变吧?
改过了,从0到3都试过了,都是提示这个。貌似有提到datetime类型,vs里面错误提示,但是从0到3都是一样提示。
可以根据取出的datatbale的compute来算和。(百度查datatable compute)
这种方式很慢,影响速度。不如在DataBound中用SQL统计呢。
谢谢,我重新写了个方法,多用了一次SQL查询来统计再显示出来了。