使用rowdatabound的列之间的总和

时间:2017-03-23 21:18:24

标签: asp.net sum rowdatabound

你好我有7列的表我想总和3号和4号和5号到6号(总计) 但它总是得到这个错误“输入字符串格式不正确”。  同样在值中注意它将是一些空值thx

数据类型是float

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string val1 = e.Row.Cells[3].Text; //Gets the value in Column 1
        string val2 = e.Row.Cells[4].Text; //Gets the value in Column 2
        string val3 = e.Row.Cells[5].Text; //Gets the value in Column 2
        Label lblTotal = (Label)e.Row.Cells[6].FindControl("Label1"); //

        if (string.IsNullOrEmpty(val1))
        { val1 = "0"; }
        if (string.IsNullOrEmpty(val2))
        { val2 = "0"; }
        if (string.IsNullOrEmpty(val3) )
        { val3 = "0"; }


        int sum = Int32.Parse(val1) + Int32.Parse(val2) + Int32.Parse(val3);
        lblTotal.Text += sum.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

不要使用单元格中的-Wl,-rpath,<directory with libpq.dll>来表示其他任何字符串。如果要在RowDataBound事件中添加值,请使用DataRowView对象。从那里你可以获得价值并使用它们。

.Text