2

我将以下 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# 很陌生,所以这将是一个很大的帮助。

4

3 回答 3

1
string.IsNullOrWhiteSpace(tabledata["1"])? 0.0m: (decimal)tabledata["1"];
于 2013-06-21T20:36:27.883 回答
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;
}
于 2013-06-21T20:45:18.750 回答
0

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

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

然后:

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

这不仅可以防止空值。

于 2013-06-21T20:49:09.370 回答