网上搜了半天,gridview的求和代码貌似都是清清月儿那个版本,可是在我这里貌似用不了,会提示“输入字符串的格式不正确”。前台:<asp:GridView ID="GridView1" runat="server" 
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="日期">                   
                    <ItemTemplate>
                        <asp:Label ID="lb_Date" runat="server" Text='<%# Bind("ExpDate") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="70px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="名称">                   
                    <ItemTemplate>
                        <asp:Label ID="lb_Name" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="70px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="数量">                   
                    <ItemTemplate>
                        <asp:Label ID="lb_result" runat="server" Text='<%# Bind("ExpResult") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="50px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="收入">                   
                    <ItemTemplate>
                        <asp:Label ID="lb_income" runat="server" Text='<%# Bind("ExpIncome") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="50px" />
                </asp:TemplateField>
            </Columns>
                <FooterStyle BackColor="White" ForeColor="#000066" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                <RowStyle ForeColor="#000066" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#007DBB" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#00547E" />
        </asp:GridView>
后台查询显示:
string sConnectionString = ConfigurationManager.ConnectionStrings["dbConn"].ToString();
                using (SqlConnection conn1 = new SqlConnection(sConnectionString))
                {
                    conn1.Open();
                    SqlDataAdapter dap = new SqlDataAdapter("SELECT * FROM [AppExp]", conn1);                    DataSet ds1 = new DataSet();
                    try
                    {
                        dap.Fill(ds1);
                    }
                    catch
                    {
                    }                    GridView1.DataSource = ds1;
                    GridView1.DataBind();
求和显示到label的代码:
private Decimal summoney = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                summoney += Convert.ToDecimal(e.Row.Cells[3].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                lb_summoney.Text = summoney.ToString();            }
        }
报错的就是上面这行:summoney += Convert.ToDecimal(e.Row.Cells[4].Text);我想问下,这个3是不是gridview显示从做往右从0开始数第3列?我上面那个确实是第3列啊?请教,谢谢。

解决方案 »

  1.   

    用js算和还有有可行性的。比如数量一列加上.amount 样式来找到对象。
    如果实在后台的话,可以根据取出的datatbale的compute来算和。(百度查datatable  compute)
    缺点是只能把当前页的数据之和都算出来, 而无法做到根据选取列来算和
      

  2.   

    gridview.rows[0].cells[0].text; //获取第一行第一列的值
    或者
    gridview.findcontrol("这里写你要获取值的那个label的id");
      

  3.   

    用JQuery操作吧。组织好dom元素的class / id<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
     <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                var varObj = $(".td1");
                var num = 0;
                varObj.each(function () {
                    num += parseInt($(this).text());
                });            $(".total").text(num);
            });
        </script><style type="text/css">
    .total{ font-size:14px; font-weight:bold; color:#f00;}
    </style>
       
    </head>
    <body>
    第一列总和:<span class="total"></span>
    <hr /><table>
    <tr>
        <td class="td1_total"></td>
        <td class="td2_total"></td> 
    </tr>
    <tr>
        <td class="td1">21</td>
        <td class="td2">36</td> 
    </tr>
    <tr>
        <td class="td1">23</td>
        <td class="td2">57</td> 
    </tr>
    <tr>
        <td class="td1">47</td>
        <td class="td2">23</td> 
    </tr>
    </table>
       
    </body>
    </html>
      

  4.   

    配合Repeater控件,一日三次,温水服用更佳
      

  5.   

    表示第四列
    你第四列是 Decimal类型的字符串吗??
      

  6.   


    从0开始数的第4列是价格,用的就是Decimal类型的字段,数据库是这样的,但是查询出来显示在gridvirw这个类型不会变吧?
      

  7.   


    改过了,从0到3都试过了,都是提示这个。貌似有提到datetime类型,vs里面错误提示,但是从0到3都是一样提示。
      

  8.   


    可以根据取出的datatbale的compute来算和。(百度查datatable  compute)
      

  9.   

    为什么是在RowDataBound中呢?
    这种方式很慢,影响速度。不如在DataBound中用SQL统计呢。
      

  10.   


    谢谢,我重新写了个方法,多用了一次SQL查询来统计再显示出来了。