如何自动计算DataGrid的小计,合计?
有Datagrid1pro_id pro_price(模板列) pro_amount(模板列) totalmoney(小计)
1 文本框 文本框
2 文本框 文本框
5 文本框 文本框
在页面上放了一个LABEL;合计总金额
当在文本框输入数值时,自动统计小计和合计金额.
请问大家是怎样实现的.谢谢大家 --------------------编程问答-------------------- 用到AJAX了 输入完后 焦点离开时 计算
如果不用AJAX 用户输入完 焦点离开 再刷新页面 很不友好
谁有现成的代码 发上来吧 --------------------编程问答-------------------- 楼上的朋友,你能说说,就算屏幕要闪动,应该怎么做? --------------------编程问答-------------------- public override void DataBind()
{
this.Dw_Ety=(SysCommon.EntityBase.BaseDataSet)this.DataSource;
//this.Columns.Clear();
if(this.Columns.Count==0)
{
//选择列
TemplateColumn colCheck=new TemplateColumn();
colCheck.ItemTemplate=new DataGridTemplate(ListItemType.SelectedItem);
colCheck.Visible=this.B_ShowCheckBox;//控制是否显示选择列
this.Columns.Add(colCheck);
//序号列
BoundColumn col1=new BoundColumn();
col1.HeaderText="序号";
col1.Visible=this.b_ShowSeq;
this.Columns.Add(col1);
#region 动态添加字段
foreach(SysCommon.EntityBase.ColumnProperty cp in Dw_Ety.ColumnPropertyList)
{
//是否显示
if(cp.B_display)
{
#region Foot信息
//添加合计列属性
if(cp.B_summary)
{
this.AddFootProperty(new FootProperty(this.Columns.Count,cp.Column));
}
#endregion
//普通-只读
if(cp.E_showType==SysCommon.EntityBase.ShowType.Common && cp.B_readonly)
{
BoundColumn col=new BoundColumn();
col.HeaderText=cp.Column_memo;
col.DataField=cp.Column;
col.Visible=cp.B_display;
this.Columns.Add(col);
}
//普通-可写
if(cp.E_showType==SysCommon.EntityBase.ShowType.Common && !cp.B_readonly)
{
TemplateColumn coltb = new TemplateColumn();
coltb.ItemTemplate =new DataGridTemplate(ListItemType.Item,cp,this.b_ReadOnly);
coltb.HeaderText = cp.Column_memo;
//coltb.FooterText="测试";
this.Columns.Add(coltb);
}
//列表
if(cp.E_showType==SysCommon.EntityBase.ShowType.DropDownList)
{
TemplateColumn collist = new TemplateColumn();
collist.ItemTemplate =new DataGridTemplate(ListItemType.Item,cp,this.b_ReadOnly);
collist.HeaderText = cp.Column_memo;
this.Columns.Add(collist);
}
//sql语句
if(cp.E_showType==SysCommon.EntityBase.ShowType.Sql)
{
TemplateColumn colsql = new TemplateColumn();
colsql.ItemTemplate =new DataGridTemplate(ListItemType.Item,cp,this.b_ReadOnly);
colsql.HeaderText = cp.Column_memo;
this.Columns.Add(colsql);
}
this.EnableViewState=true;
this.ItemDataBound+=new DataGridItemEventHandler(DataWindow_ItemDataBound);
this.ItemCommand+=new DataGridCommandEventHandler(DataWindow_ItemCommand);
}
}
#endregion
}
base.DataBind ();
}
//BF层命名空间
private string str_business_namespace="Business";
#region Foot信息
private System.Collections.ArrayList o_footPropertyList;
private void AddFootProperty(FootProperty ft)
{
this.o_footPropertyList.Add(ft);
}
/// <summary>
/// 根据字段名称返回Foot合计信息
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
private FootProperty GetFootPropertyByColumn(string column)
{
foreach(FootProperty fp in this.o_footPropertyList)
{
if(fp.Str_column==column)
{
return fp;
}
}
return null;
}
/// <summary>
/// 根据字段序号返回Foot合计信息
/// </summary>
/// <param name="seq"></param>
/// <returns></returns>
private FootProperty GetFootPropertyBySeq(int seq)
{
foreach(FootProperty fp in this.o_footPropertyList)
{
if(fp.I_footCellSeq==seq)
{
return fp;
}
}
return null;
}
#endregion
#region 加载DropDownList数据
/// <summary>
/// 加载DropDownList数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataWindow_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)
{
foreach(SysCommon.EntityBase.ColumnProperty cp in Dw_Ety.ColumnPropertyList)
{
//绑定下拉数据
if(cp.E_showType==SysCommon.EntityBase.ShowType.DropDownList || cp.E_showType==SysCommon.EntityBase.ShowType.Sql)
{
DropDownList ddl=(DropDownList)e.Item.FindControl(cp.Column);
BindDropDownList(cp,ddl,e);
}
}
//序号
if(e.Item.ItemIndex!=-1)
{
//1固定是序号
e.Item.Cells[1].Text=(e.Item.ItemIndex + 1).ToString();
}
//交替变色
if (e.Item.ItemIndex >= 0)
{
e.Item.Attributes["onMouseOver"]= "javascript:this.bgColor='#ccccff';";
e.Item.Attributes["onMouseOut"] = "javascript:this.bgColor='#ffffff';";
e.Item.Attributes["Style"]="cursor:hand";
}
}
if(e.Item.ItemType==ListItemType.Footer)
{
#region Foot信息
//绑定下拉数据
e.Item.Cells[0].Text = "合计:";
foreach(FootProperty fp in this.o_footPropertyList)
{
e.Item.Cells[fp.I_footCellSeq].Text = this.Dw_Ety.DefaultTable.Compute("sum("+fp.Str_column+")","").ToString();
}
#endregion
}
} --------------------编程问答-------------------- 上面是合计,如果是小计的话,只需要修改你的DataSet就可以了,举例如下:
-----------
column = columns.Add ( FIELD_retail_a, typeof(System.Decimal) );
column.Caption = "批发金额";
//column.Expression="q * retail_price";
//ColumnProperty(string str_column,string str_type,string str_memo,ShowType.Common,string str_dropSql,string str_drop_table_name,string str_dropValue,string str_dropText,string[] str_enum,bool display,bool b_Readonly)
ColumnPropertyList.Add(new ColumnProperty("retail_a","decimal","批发金额",ShowType.Common,"retail_a",null,"ID","Name",null,true,false,true));
//*********************************************
//添加自定义的计算列
//要求将只读属性设置为只读
//*********************************************
column=columns.Add("grass_a",typeof(System.Decimal));
column.Caption="毛利";
column.Expression = "retail_a - entry_a";
ColumnPropertyList.Add(new ColumnProperty("grass_a","decimal","毛利",ShowType.Common,"grass_a",null,"ID","Name",null,true,true,true));
//********************************************* --------------------编程问答-------------------- 谢谢楼上的朋友,有点看不懂,能说说原理吗? --------------------编程问答-------------------- 合计主要是用到DataGrid的Footer
-----------------------------------------
if(e.Item.ItemType==ListItemType.Footer)
{
#region Foot信息
e.Item.Cells[0].Text = "合计:";
foreach(FootProperty fp in this.o_footPropertyList)
{
e.Item.Cells[fp.I_footCellSeq].Text = this.Dw_Ety.DefaultTable.Compute("sum("+fp.Str_column+")","").ToString();
}
------------------------------------
this.Dw_Ety是我放到ViewState里的自定义的DataSet
----------------------------------------
如果那一列需要合计的话则记录合计信息
if(cp.B_summary)
{
this.AddFootProperty(new FootProperty(this.Columns.Count,cp.Column));
}
--------------------编程问答-------------------- 小记则用到的是DataColumn的Expression属性
//*********************************************
//添加自定义的计算列
//*********************************************
column=columns.Add("grass_a",typeof(System.Decimal));
column.Caption="毛利";
column.Expression = "retail_a - entry_a";
ColumnPropertyList.Add(new ColumnProperty("grass_a","decimal","毛利",ShowType.Common,"grass_a",null,"ID","Name",null,true,true,true));
-------------------
关键是这一句
column.Expression = "retail_a - entry_a"; --------------------编程问答-------------------- this.Dw_Ety.DefaultTable.Compute("sum("+fp.Str_column+")","").ToString();
--------
这里的DefaultTable是自定义DataSet里的表,DataTable有一个Compute函数,你查下msdn就明白了 --------------------编程问答-------------------- 谢谢楼上的 ! --------------------编程问答-------------------- 还是没明白
补充:.NET技术 , ASP.NET