【问题标题】:Sum cell values when some are null and display in footer of asp.net gridview当一些单元格值为空时求和单元格值并显示在 asp.net gridview 的页脚中
【发布时间】:2013-06-21 20:32:49
【问题描述】:

我将以下 asp GridView 绑定到 SqlDataSource:

    <asp:GridView ID="LaborGrid" runat="server" AutoGenerateColumns="False" DataSourceID="LaborDS" CssClass="budgetGrid" AllowSorting="True" OnRowDataBound="LaborGrid_RowDataBound" ShowFooter="true" >
    <Columns>
        <asp:BoundField DataField="Account Name" HeaderText="Account Name" SortExpression="Account Name" ItemStyle-CssClass="budgetTitle" />
        <asp:BoundField DataField="1" HeaderText="1" DataFormatString="{0:C}" SortExpression="1" ReadOnly="True" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="2" HeaderText="2" DataFormatString="{0:C}" ReadOnly="True" SortExpression="2" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="3" HeaderText="3" DataFormatString="{0:C}" ReadOnly="True" SortExpression="3" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="4" HeaderText="4" DataFormatString="{0:C}" ReadOnly="True" SortExpression="4" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="5" HeaderText="5" DataFormatString="{0:C}" ReadOnly="True" SortExpression="5" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="6" HeaderText="6" DataFormatString="{0:C}" ReadOnly="True" SortExpression="6" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="7" HeaderText="7" DataFormatString="{0:C}" ReadOnly="True" SortExpression="7" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="8" HeaderText="8" DataFormatString="{0:C}" ReadOnly="True" SortExpression="8" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="9" HeaderText="9" DataFormatString="{0:C}" ReadOnly="True" SortExpression="9" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="10" HeaderText="10" DataFormatString="{0:C}" ReadOnly="True" SortExpression="10" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="11" HeaderText="11" DataFormatString="{0:C}" ReadOnly="True" SortExpression="11" ItemStyle-CssClass="budgetCell" />
        <asp:BoundField DataField="12" HeaderText="12" DataFormatString="{0:C}" ReadOnly="True" SortExpression="12" ItemStyle-CssClass="budgetCell" />
    </Columns>
</asp:GridView>

这就是我试图对这些值求和的方法:

public partial class labor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    decimal pd1 = 0m;
    decimal pd2 = 0m;
    decimal pd3 = 0m;
    decimal pd4 = 0m;
    decimal pd5 = 0m;
    decimal pd6 = 0m;
    decimal pd7 = 0m;
    decimal pd8 = 0m;
    decimal pd9 = 0m;
    decimal pd10 = 0m;
    decimal pd11 = 0m;
    decimal pd12 = 0m;

    protected void LaborGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView tabledata = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            pd1 += (decimal)tabledata["1"];
            // pd1 += decimal.TryParse(tabledata["1"], out tempValue) ? tempValue : 0.0;
            pd2 += (decimal)tabledata["2"];
            pd3 += (decimal)tabledata["3"];
            pd4 += (decimal)tabledata["4"];
            pd5 += (decimal)tabledata["5"];
            pd6 += (decimal)tabledata["6"];
            pd7 += (decimal)tabledata["7"];
            pd8 += (decimal)tabledata["8"];
            pd9 += (decimal)tabledata["9"];
            pd10 += (decimal)tabledata["10"];
            pd11 += (decimal)tabledata["11"];
            pd12 += (decimal)tabledata["12"];
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[1].Text = pd1.ToString("C");
            e.Row.Cells[2].Text = pd2.ToString("C");
            e.Row.Cells[3].Text = pd3.ToString("C");
            e.Row.Cells[4].Text = pd4.ToString("C");
            e.Row.Cells[5].Text = pd5.ToString("C");
            e.Row.Cells[6].Text = pd6.ToString("C");
            e.Row.Cells[7].Text = pd7.ToString("C");
            e.Row.Cells[8].Text = pd8.ToString("C");
            e.Row.Cells[9].Text = pd9.ToString("C");
            e.Row.Cells[10].Text = pd10.ToString("C");
            e.Row.Cells[11].Text = pd11.ToString("C");
            e.Row.Cells[12].Text = pd12.ToString("C");
        }

    }
}

但我在 pd1 += (decimal)tabledata["1"]; 处不断收到“Specified cast is not valid”; 返回的一些值是 NULL。检查空值然后添加到每个外部变量的语法是什么?

我是 asp.net/C# 的新手,所以这将是一个很大的帮助。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我喜欢加里森使用IsNullOrWhiteSpace

    在此基础上,我为您的项目编写了这个静态辅助函数:

    private static decimal Read(object value) {
      if ((value != null) && (value != DBNull.Value)) {
        string strCast = value.ToString();
        if (!String.IsNullOrWhiteSpace(strCast)) {
          return Convert.ToDecimal(value);
        }
      }
      return 0.0m;
    }
    

    【讨论】:

    • 很高兴为您提供帮助。郑重声明,您的值很可能是 DBNull.Value - 如果您不习惯数据库,这些值可能会让您大吃一惊。
    【解决方案2】:
    string.IsNullOrWhiteSpace(tabledata["1"])? 0.0m: (decimal)tabledata["1"];
    

    【讨论】:

      【解决方案3】:

      我想创建一个单独的方法。

      private decimal getValue(string s)
      {
          decimal returnValue;
          if(!(decimal.TryParse(s, out returnValue)))
          {
              returnValue = 0;
          }
          return returnValue;
      }
      

      然后:

      pd1 += getValue(tabledata["1"]);
      

      这不仅可以防止空值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-05
        • 2013-09-20
        • 1970-01-01
        相关资源
        最近更新 更多