• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

DATAGRID的页脚处增加合计的问题

dallascen 2004-11-02 12:01:34
请问各位,表test的结构如下:

aaa bbb
名称1 金额1
名称2 金额2


想用DATAGRID 显示成

aaa bbb
名称1 金额1
名称2 金额2
合计 金额1+金额2

请问各位高人,这个用vb.net怎么实现呢。。。。
...全文
297 点赞 收藏 16
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
dallascen 2004-11-07
Kiven1978(谁隐)的方法确实好用啊,谢谢了,分已经给了啊,多谢;)
回复
pycys001 2004-11-02
刚好要搞这个
回复
frankyshu 2004-11-02
学习
回复
李天平 2004-11-02
1,显示页脚

2,
if((e.Item.ItemType==ListItemType.Item)||(e.Item.ItemType==ListItemType.AlternatingItem))
{
Hourssum+=s.Hours;
}

3,
if(e.Item.ItemType==ListItemType.Footer)
{
e.Item.Cells[9].Text=Hourssum.ToString();
}
回复
SEVEN_QI_7 2004-11-02
这是一个很简单的方法啊!!!!
回复
SEVEN_QI_7 2004-11-02
Private Sub dgr1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgr1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim viewCount As Decimal = Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "bjd_je"))
viewstate("viewCountSum1") += viewCount
ElseIf e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(4).HorizontalAlign = HorizontalAlign.Center
e.Item.Cells(4).Text = "合计"
e.Item.Cells(5).HorizontalAlign = HorizontalAlign.Right
e.Item.Cells(5).Text = FormatNumber(viewstate("viewCountSum1"), 2)
viewstate("viewCountSum1") = 0
End If
End Sub
回复
li_new 2004-11-02
我的实际项目中用的:
footer要有,其实合计的行就是利用footer;
下面是代码:
private void dgGoodsDetail_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
System.Data.DataRowView dbr;
switch(e.Item.ItemType)
{
case ListItemType.Item:
dbr = (System.Data.DataRowView)e.Item.DataItem;
totalNumber = totalNumber + int.Parse(getTextValue(dbr["Number"].ToString()));
totalInSurance = totalInSurance + double.Parse(getTextValue(dbr["InSurance"].ToString()));
totalPayFee = totalPayFee + double.Parse(getTextValue(dbr["PayFee"].ToString()));
totalLeftFee = totalLeftFee + double.Parse(getTextValue(dbr["LeftFee"].ToString()));
totalShouldPay = totalShouldPay + double.Parse(getTextValue(dbr["ShouldPay"].ToString()));
totalTransferFee = totalTransferFee + double.Parse(getTextValue(dbr["TransferFee"].ToString()));
break;
case ListItemType.AlternatingItem:
dbr = (System.Data.DataRowView)e.Item.DataItem;
totalNumber = totalNumber + int.Parse(getTextValue(dbr["Number"].ToString()));
totalInSurance = totalInSurance + double.Parse(getTextValue(dbr["InSurance"].ToString()));
totalPayFee = totalPayFee + double.Parse(getTextValue(dbr["PayFee"].ToString()));
totalLeftFee = totalLeftFee + double.Parse(getTextValue(dbr["LeftFee"].ToString()));
totalShouldPay = totalShouldPay + double.Parse(getTextValue(dbr["ShouldPay"].ToString()));
totalTransferFee = totalTransferFee + double.Parse(getTextValue(dbr["TransferFee"].ToString()));
break;
case ListItemType.Footer:
//共10列
//0列
Literal ltl1 = new Literal();
ltl1.Text = "汇  总";
e.Item.Cells[0].Controls.Add(ltl1);
e.Item.Cells[0].Font.Bold = true;
e.Item.Cells[0].ForeColor = System.Drawing.Color.Black;
e.Item.Cells[0].ColumnSpan = 5;
e.Item.Cells[0].HorizontalAlign = HorizontalAlign.Center;
e.Item.Cells.Remove(e.Item.Cells[1]);

//4列
Literal ltl3 = new Literal();
ltl3.Text = totalNumber.ToString();
e.Item.Cells[4].Controls.Add(ltl3);
e.Item.Cells[4].HorizontalAlign = HorizontalAlign.Right;

//5列
Literal ltl4 = new Literal();
if(totalInSurance == 0)
{
ltl4.Text = "0";
}
else
{
ltl4.Text = string.Format("{0:F2}",totalInSurance);
}
e.Item.Cells[5].Controls.Add(ltl4);
e.Item.Cells[5].HorizontalAlign = HorizontalAlign.Right;
//6列
Literal ltl5 = new Literal();
if (totalPayFee == 0)
{
ltl5.Text = "0";
}
else
{
ltl5.Text = string.Format("{0:F2}",totalPayFee);
}
e.Item.Cells[6].Controls.Add(ltl5);
e.Item.Cells[6].HorizontalAlign =HorizontalAlign.Right;
//7列
Literal ltl6 = new Literal();
if (totalLeftFee == 0)
{
ltl6.Text = "0";
}
else
{
ltl6.Text = string.Format("{0:F2}",totalLeftFee);
}
e.Item.Cells[7].Controls.Add(ltl6);
e.Item.Cells[7].HorizontalAlign = HorizontalAlign.Right;
//8列
Literal ltl7 = new Literal();
if (totalShouldPay == 0)
{
ltl7.Text = "0";
}
else
{
ltl7.Text = string.Format("{0:F2}",totalShouldPay);
}
e.Item.Cells[8].Controls.Add(ltl7);
e.Item.Cells[8].HorizontalAlign = HorizontalAlign.Right;
//9列
Literal ltl8 = new Literal();
if (totalTransferFee == 0)
{
ltl8.Text = "0";
}
else
{
ltl8.Text = String.Format("{0:F2}",totalTransferFee);
}
e.Item.Cells[9].Controls.Add(ltl8);
e.Item.Cells[9].HorizontalAlign = HorizontalAlign.Right;

e.Item.Cells.Remove(e.Item.Cells[1]);
e.Item.Cells.Remove(e.Item.Cells[1]);
e.Item.Cells.Remove(e.Item.Cells[1]);
break;
}
}

private string getTextValue(string source)
{
if (source == string.Empty)
{
return "0";
}
else
{
return source;
}
}
回复
happyjun2000 2004-11-02
up
回复
netDust_cv 2004-11-02
方法如下:

直接用SQL语句算出SUM的值就可以了啊:)
然后将值用DataTable的myDataTable.Rows.Add方法添加到表的最后一行就OK了。
回复
goody9807 2004-11-02
http://dotnet.aspx.cc/ShowDetail.aspx?id=1E0710F9-29CE-4425-90BB-3553F5C820DA
回复
cansum396 2004-11-02
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim Obj As Object
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Obj = e.Item.Cells(8).Controls(1) '本次还款金额
If e.Item.ItemIndex <= Tb_Customer_Pay.Rows.Count - 1 Then
Obj.Attributes.Add("onkeydown", "if(event.keyCode==13) {if(" + e.Item.Cells(8).Controls(1).ClientID + ".value =='') {alert('本次还款金额不能为空!');return(false)}{if(isNaN(" + e.Item.Cells(8).Controls(1).ClientID + ".value)){" + e.Item.Cells(8).Controls(1).ClientID + ".value='';alert('本次还款金额请填写数字');return(false)}{if(parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value)>parseFloat(" + e.Item.Cells(7).Text + ")) {alert('本次还款金额不能大于余额!');return(false)}{" + e.Item.Cells(9).Controls(1).ClientID + ".value=parseFloat(" + e.Item.Cells(7).Text + ")-parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value);var total=0;var rowcount=" + (Tb_Customer_Pay.Rows.Count - 1).ToString + "; for(var i = 0; i<=rowcount; i++){total=total+parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value);};if(parseFloat(total)>(parseFloat(document.getElementById('Txt_Pay_Amount').value)*parseFloat(document.getElementById('Txt_Curr_Rate').value))) {alert('分配的本次还款金额大于还款总金额,请核对!') } else {document.getElementById('DataGrid1__ctl" + Trim(Str(Tb_Customer_Pay.Rows.Count - 1 + 2 + 1)) + "_foot').value=total}}}}}")
'在页脚foot控件里放入合计数
Else
Obj.Attributes.Add("onkeydown", "if(event.keyCode==13) {if(" + e.Item.Cells(8).Controls(1).ClientID + ".value =='') {alert('本次还款金额不能为空!');return(false)}{if(isNaN(" + e.Item.Cells(8).Controls(1).ClientID + ".value)){" + e.Item.Cells(8).Controls(1).ClientID + ".value='';alert('本次还款金额请填写数字');return(false)}{if(parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value)>parseFloat(" + e.Item.Cells(7).Text + ")) {alert('本次还款金额不能大于余额!');return(false)}{" + e.Item.Cells(9).Controls(1).ClientID + ".value=parseFloat(" + e.Item.Cells(7).Text + ")-parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value);var total=0;var rowcount=" + (Tb_Customer_Pay.Rows.Count - 1).ToString + "; for(var i = 0; i<=rowcount; i++){total=total+parseFloat(" + e.Item.Cells(8).Controls(1).ClientID + ".value);};if(parseFloat(total)>(parseFloat(document.getElementById('Txt_Pay_Amount').value)*parseFloat(document.getElementById('Txt_Curr_Rate').value))) {alert('分配的本次还款金额大于还款总金额,请核对!') } else {document.getElementById('DataGrid1__ctl" + Trim(Str(Tb_Customer_Pay.Rows.Count - 1 + 2 + 1)) + "_foot').value=total;document.getElementById('DataGrid1__ctl" + Trim(Str(e.Item.ItemIndex + 2 + 1)) + "_TextBox1').focus();document.getElementById('DataGrid1__ctl" + Trim(Str(e.Item.ItemIndex + 2 + 1)) + "_TextBox1').select()}}}}}")
End If
total += Tb_Customer_Pay.Rows(e.Item.ItemIndex).Item("CURR_PAY")
End If
If e.Item.ItemType = ListItemType.Footer Then
Obj = e.Item.FindControl("foot")
Obj.text = total.Round(total, 2)
End If
End Sub

用js脚本实现在客户端的实时得到合计数(VB.net)
回复
tongcheng 2004-11-02
看看
回复
karykwan 2004-11-02
http://authors.aspalliance.com/olson/code/summary/Summary3.aspx
这是和你要的一个样
回复
rustical 2004-11-02

Private Sub GrjfGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles GrjfGrid.ItemDataBound
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(0).Text = "欠费合计"
e.Item.Cells(1).Text = TotalBs.ToString & "笔"
e.Item.Cells(2).Text = (TotalGrjf + TotalDwhb + Total).ToString & "元"
e.Item.Cells(5).Text = TotalGrjf.ToString & "元"
e.Item.Cells(6).Text = TotalDwhb.ToString & "元"
e.Item.Cells(7).Text = Total.ToString & "元"
End If

在 datagrid 绑定前请执行 dataset 的方法:
TotalGrjf = CDbl(SelTable.Compute("Sum(gryjje)", "status=" & QFFLAG))
TotalDwhb = CDbl(SelTable.Compute("Sum(dwhbje)", "status =" & QFFLAG))
Total = CDbl(SelTable.Compute("Sum(byjfhj)", "status=" & QFFLAG))
回复
saucer 2004-11-02
see

HOW TO: Create a Summary Row for a DataGrid in ASP.NET by Using Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q313154

Summary Rows in a DataGrid: A Comparison of Techniques
http://authors.aspalliance.com/olson/articles/summary.aspx

use the above code, you could also use

object o = YourDataTable.Compute("SUM(bbb)", "");
e.Item.Cells(0).Text = o.ToString();

回复
dallascen 2004-11-02
自己顶一下。。。
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-11-02 12:01
社区公告
暂无公告