asp.net 中如何得到GridView 中某一列的值的和

chbklmt 2009-10-07 04:52:22
如题 下面是代码 可以按月份查找出来,什么把其中一列的值的总和求出来最后面的那个按钮还空着请各位大虾帮忙看看
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.Common;
using System.Text;

public partial class admin_SelectIntoSingle : System.Web.UI.Page
{



protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

MyDataBind();
}

}
public void MyDataBind()
{
OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("access.mdb"));
OleDbDataAdapter MyCommand = new OleDbDataAdapter(@"SELECT * FROM [Into Single] ", MyConnection);
DataSet ds = new DataSet();

try
{
MyCommand.Fill(ds, "access");
GridView_IntoSingle.DataSource = ds.Tables[0];
GridView_IntoSingle.DataBind();


}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
MyConnection.Close();
MyConnection.Dispose();
MyConnection = null;
}
}

protected void GridView_IntoSingle_PageIndexChanging(object sender, GridViewPageEventArgs e)
{



GridView_IntoSingle.SelectedIndex = -1;
GridView_IntoSingle.PageIndex = e.NewPageIndex;
//GridView_IntoSingle.DataBind();
MyDataBind();
}

protected void Button_selectbytime_Click(object sender, EventArgs e)
{
// this.TextBox_Time.Text = this.TextBox_Time.Text + "-";
string BookOrderTime = "-" + this.TextBox_Time.Text +"-";
OleDbConnection MyConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("access.mdb"));
OleDbDataAdapter cmd = new OleDbDataAdapter(@"select * from [Into Single] where BookOrderTime like '%" + BookOrderTime+ "%'", MyConnection);
DataSet ds = new DataSet();
OleDbParameter p1 = new OleDbParameter("@BookOrderTime",OleDbType.VarChar);
p1.Value = BookOrderTime;
cmd.SelectCommand.Parameters.Add(p1) ;
try
{
cmd.Fill(ds, "access");
GridView_IntoSingle.DataSource = ds.Tables[0];
GridView_IntoSingle.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
MyConnection.Close();
MyConnection.Dispose();
MyConnection = null;
}

}
protected void Button_cencel_Click(object sender, EventArgs e)
{
this.TextBox_Time.Text = "";
MyDataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
this.TextBox2.Text = "";
MyDataBind();
}
protected void Button_money_Click(object sender, EventArgs e)
{


}
protected void GridView_IntoSingle_SelectedIndexChanged(object sender, EventArgs e)
{


}
}

...全文
400 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
namhyuk 2009-10-08
  • 打赏
  • 举报
回复
分页的情况下你当然得手动计算了. 计算后的结果放到
GridView1.FooterRow里的某个控件不就得了吗?

手动计算什么意思啊? 就是SQL语句里的sum(字段名), 返回来再显示出来呗.
chbklmt 2009-10-08
  • 打赏
  • 举报
回复
按上面做是可以在下面显示出来结果, 现在先用着,可不还有些不完美,这样只能算当前页面的结果,如果我有两个页面或三个页面的话。。。
我是想做个按钮 可以将查找到的全显示出来 就算不在当前页面,而是在第二页第三页!
还有谢谢两个给的修改代码 谢谢
  • 打赏
  • 举报
回复
借用#2楼的demo例子做个变动:
<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Calc1((decimal)Eval("UnitPrice")) %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LabelSum" runat="server" Text='<%# sum %>' />
</FooterTemplate>
</asp:TemplateField>


private decimal sum = 0m;

protected decimal Calc1(decimal x)
{
sum += x;
return x;
}


绑定UnitPrice的值的时候,程序拿过来用一次,输入的是x,输出也还是x,只是把x累加到sum上。
happy664618843 2009-10-07
  • 打赏
  • 举报
回复
路过 顶顶
che2piaopiao 2009-10-07
  • 打赏
  • 举报
回复
取出,循环,相加。
namhyuk 2009-10-07
  • 打赏
  • 举报
回复
UnitPrice表->UnitPrice列
namhyuk 2009-10-07
  • 打赏
  • 举报
回复
以Northwind数据库的Products表的UnitPrice表为例.
GridView1.ShowFooterRow = True;
把UnitPrice列变成模板列.

<asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LabelSum" runat="server" />
</FooterTemplate>
</asp:TemplateField>



private decimal sum = 0m;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView drv = e.Row.DataItem as System.Data.DataRowView;
sum += Convert.ToDecimal(drv["UnitPrice"]);
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
Label labelSum = GridView1.FooterRow.FindControl("LabelSum") as Label;
labelSum.Text = sum.ToString();
}
PandaIT 2009-10-07
  • 打赏
  • 举报
回复
网上代码太多了!

简单说下实现过程

在gridview的rowdatabound事件中进行列值的累加

然后在脚注中显示!!

<footerTemplate>
//...code
</footerTemplate>

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧