国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在datagrid中求和(vb.net,c#)

2024-07-10 12:58:27
字體:
來源:轉載
供稿:網友
aspx文件
<%@ page inherits="myapp.calctotals" src="" %>
<!--自己改一下src-->
<html>
<body bgcolor="white">
<asp:datagrid id="mygrid" runat="server"
  autogeneratecolumns="false"
  cellpadding="4" cellspacing="0"
  borderstyle="solid" borderwidth="1"
  gridlines="none" bordercolor="black"
  itemstyle-font-name="verdana"
  itemstyle-font-size="9pt"
  headerstyle-font-name="verdana"
  headerstyle-font-size="10pt"
  headerstyle-font-bold="true"
  headerstyle-forecolor="white"
  headerstyle-backcolor="blue"
  footerstyle-font-name="verdana"
  footerstyle-font-size="10pt"
  footerstyle-font-bold="true"
  footerstyle-forecolor="white"
  footerstyle-backcolor="blue"
  onitemdatabound="mydatagrid_itemdatabound"
  showfooter="true">
<!--在footer中顯示合計-->
  <columns>
    <asp:boundcolumn headertext="title" datafield="title" />
    <asp:boundcolumn headertext="price" datafield="price"
      itemstyle-horizontalalign="right"
      headerstyle-horizontalalign="center" />
  </columns>
</asp:datagrid>
</body>
</html>
下面給出vb.net和c#兩種代碼
vb.net
imports system
imports system.web
imports system.web.ui
imports system.web.ui.webcontrols
imports system.web.ui.htmlcontrols
imports system.data
imports system.data.sqlclient

namespace myapp

  public class calctotals : inherits page
  
    protected mygrid as datagrid
    private runningtotal as double = 0

'定義合計變量
    private sub calctotal(_price as string)
'求和
      try
        runningtotal += double.parse(_price)
      catch
        ' 空值
      end try
    end sub

    public sub mydatagrid_itemdatabound(sender as object, e as datagriditemeventargs)

      if e.item.itemtype = listitemtype.item or e.item.itemtype = listitemtype.alternatingitem then

        calctotal( e.item.cells(1).text )
'循環執行求和程序
        e.item.cells(1).text = string.format("{0:c}", convert.todouble(e.item.cells(1).text))

      elseif(e.item.itemtype = listitemtype.footer )

        e.item.cells(0).text="total"
        e.item.cells(1).text = string.format("{0:c}", runningtotal)
      end if
    end sub

    protected sub page_load(sender as object, e as eventargs)
    
      dim myconnection as new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=")
      dim mycommand as new sqlcommand("select title, price from titles where price > 0", myconnection)
      
      try

        myconnection.open()
        mygrid.datasource = mycommand.executereader()
        mygrid.databind()
        myconnection.close()

      catch ex as exception

        ' 有錯誤發生
        httpcontext.current.response.write(ex.tostring())
      end try
    end sub
  end class
end namespace


c#道理和vb.net是一樣的就多做解釋了
using system;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data;
using system.data.sqlclient;

namespace myapp
{
  public class calctotals : page
  {
    protected datagrid mygrid;
    private double runningtotal = 0;

    private void calctotal(string _price)
    {
      try
      {
        runningtotal += double.parse(_price);
      }
      catch
      {      
      }
    }

    public void mydatagrid_itemdatabound(object sender, datagriditemeventargs e)
    {
      if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
      {
        calctotal( e.item.cells[1].text );
        e.item.cells[1].text = string.format("{0:c}", convert.todouble(e.item.cells[1].text));
      }
      else if(e.item.itemtype == listitemtype.footer )
      {
        e.item.cells[0].text="total";
        e.item.cells[1].text = string.format("{0:c}", runningtotal);
      }  
    }

    protected void page_load(object sender, eventargs e)
    {
      sqlconnection myconnection = new sqlconnection("server=localhost;database=pubs;uid=sa;pwd=;");
      sqlcommand mycommand = new sqlcommand("select title, price from titles where price > 0", myconnection);
      
      try
      {
        myconnection.open();
        mygrid.datasource = mycommand.executereader();
        mygrid.databind();
        myconnection.close();
      }
      catch(exception ex)
      {
        httpcontext.current.response.write(ex.tostring());
      }
    }
  }
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁阳县| 剑阁县| 资讯 | 红桥区| 桐梓县| 克山县| 邯郸县| 四会市| 开远市| 黎城县| 枞阳县| 孟津县| 北流市| 长宁县| 定兴县| 六枝特区| 芒康县| 饶平县| 康马县| 惠来县| 平定县| 江油市| 沂源县| 武威市| 沾化县| 和硕县| 襄汾县| 宁海县| 金寨县| 西华县| 上林县| 土默特右旗| 巩义市| 肥东县| 同江市| 工布江达县| 张北县| 弥渡县| 阳曲县| 松阳县| 广宗县|