用union,例如sql语句:select 字段1,字段2 from yourTable where ...
union
select sum(字段1),sum(字段2) from yourTable where ...
union
select sum(字段1),sum(字段2) from yourTable where ...
解决方案 »
- 设置超时
- PostBackUrl页面传值的问题
- 用asp.net操作word如何??再现等待!!
- 提供asp,asp.net动态网页主机空间20元起,支持access数据库
- xml文件内怎么放HMTL及引号之类的东东
- 请求高手指点:DataView产生的数据源怎么绑定到Dropdownlist上面.为什么我绑定的是时候显示出来的是DataView控件名.
- 关于Cookie的奇怪问题,望高手解决
- 菜鸟:请问如何动态改变IE标题?
- 急!!!我给高分!!!一定!!谢谢了
- DataList如何判断当前行的某个字段是否为空?
- Function Sup 之间的参数传递
- hpyerlink.navigaterurl的问题,急急!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[6].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水为:";
e.Row.Cells[6].Text = sum.ToString();
e.Row.Cells[3].Text = "平均薪水为:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}后台全部代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;public partial class Default7 : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind(); }
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 飞狐工作室 set 姓名='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',家庭住址='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where 身份证号码='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
public void bind()
{
string sqlstr = "select top 5 * from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飞狐工作室");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份证号码" };
GridView1.DataBind();
sqlcon.Close();
}
private double sum = 0;//取指定列的数据和
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[6].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水为:";
e.Row.Cells[6].Text = sum.ToString();
e.Row.Cells[3].Text = "平均薪水为:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
}
前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" >
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="姓名" />
<asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
<asp:BoundField DataField="起薪" HeaderText="起薪" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
union
select sum(字段1),sum(字段2) from yourTable where ...
完全可以
gridview的第38页的最后一行就是统计值!!
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
wastageqty += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "wastage_qty"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{ // e.Row.Cells[5].Text = "单耗合计:"; //在Footer里对单耗列汇总
e.Row.Cells[6].Text = wastageqty.ToString();
wastageqty = 0; }
}
protected string Sumqty()
{
return dtd.Compute("sum(mat_qty)", "").ToString();
}
dtd是你gridvivew绑定和datatable,mat_qty是列名
合计:
</FooterTemplate>而在相应的数据列的模板插入:<FooterTemplate>
<asp:Label runat="server" Text="<%# this.Sumqty() %>" />
</FooterTemplate>如果有多个数据列都要统计,同时你要仅访问后台数据一次,那么可能需要在 DataBinding 事件中先进行统计,将结果写入一个声明为private DataRow的变量(或者Dictionary<string object>等等)保存每一个列名称与统计值的对应数据,而在Sumqty方法中提供一个列名称来查询,在界面设计声明时可以直接写::<FooterTemplate>
<asp:Label runat="server" Text='<%# this.Sumqty("fld_name") %>' />
</FooterTemplate>
使用声明风格,尽量少写代码。
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[2].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总和为:";
e.Row.Cells[6].Text = sum.ToString();
e.Row.Cells[3].Text = "平均值为:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
}
能问一下,Sumqty怎么定义?能给提供一个详细代码的定义吗?
是什么问题??
select 字段1,字段2 from yourTable where ...
union
select sum(字段1),sum(字段2) from yourTable where ...
{
e.Command.CommandText += " where 1 = 1 ";
if (TextBox1.Text != "")
{
e.Command.CommandText += " and XIO like '%" + TextBox1.Text.Trim() + "%'";
}
}
我绑定上面的代码,使用union不太方便。
{
e.Command.CommandText += " where 1 = 1 ";
if (TextBox1.Text != "")
{
e.Command.CommandText += " and XIO like '%" + TextBox1.Text.Trim() + "%'";
}
}
我绑定上面的代码,使用union不太方便。
SqlDataSource对于楼主的问题可以通过界面设置也可以用代码打入,例如:
代码方式:
SqlDataSource1.SelectCommand = "SELECT name, score1, score2 FROM Poll union select '总计',sum(score1),sum(score2) FROM Poll";